3
<div class="nav-menu clearfix">
    <ul class="list-unstyled list-inline">
        {{#each MI in MB}}
            <li id=""><span>{{ MI.MText }}</span></li>
        {{else}}
            {{MB}}
        {{/each}}
    </ul>
</div>

I want loop the MB with each.

but it dose not work and return the MB is empty.

but I get this for else.

[object Object],[object Object],[object Object],[object Object],[object Object]

this is my route define

//application路由
App.ApplicationRoute = Ember.Route.extend(App.LazyLoadTemplate, {
    model : function() {
        return Ember.$.getJSON("/index/indexjson");
    }
});

and get a json data is

{
    'UIB':{...},
    'MB':[{...},{...},...,{...}]
}

My English is not well ,Thanks for help!

1
  • I did not define any thing without route. Commented Nov 19, 2013 at 1:48

2 Answers 2

3

As you already found, your scope of your property was incorrect. Just to set the record straight, Ember-Handlebars will not iterate over an object like it will over an Array. Additionally the else statement is totally legit, it will be triggered when the array being iterated across is empty (null/undefined/no elements)

{{#each item in model}}
   {{item}}
{{else}}
   No Items
{{/each}}

http://emberjs.jsbin.com/iWohUlE/1/edit

Sign up to request clarification or add additional context in comments.

1 Comment

can I ask you one more question?how can I make sure the routeName that I have is in url currentRoutePath
0

Edit as kingpin pointed out in his answer, this response is referencing the wrong version of handlebars.

Your misusing the block helper each in your template. Handlebars each will work similarly with objects and arrays. In addition you had an else outside of an if block. Rewrite your template as follows:

<div class="nav-menu clearfix">
    <ul class="list-unstyled list-inline">
        {{#each MB}}
            <li id=""><span>{{ MText }}</span></li>
        {{/each}}
    </ul>
</div>

Fiddle example

1 Comment

Handlebars in Ember do not work similarly with objects and arrays, and the else is a valid syntax in the each helper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.