无法在模板中循环遍历Ember.Deferred Object

时间:2014-02-13 15:10:05

标签: ember.js

我想展示我从Ember.Deferred解决的问题。 并将其传递给控制器​​。但它显示错误。

<script type="text/x-handlebars" id="magazines/show/issues">
{{#if issues}}

    {{#each issue in issues}}
      <li class="has-sub">
        <a href="#">{{issue.name}}</a>
        <ul style="display: none;">
            <li {{action showGenericData issue}}><a href="#">Generic</a></li>
            <li><a href="#">Inside</a></li>
        </ul>
      </li>
    {{/each}}

{{else}}

    <li>No Issue</li>

{{/if}}

模型代码

var dfd = Ember.Deferred.create();
        var issues = Em.A();

        $.getJSON( App._api_path +  "issues/" + mag_id , 
        function(response){

            response.forEach(function(m){
                issues.pushObject(App.Issue.create(m));
            });

            dfd.resolve(issues);
        });

        return dfd;

控制台出错

Assertion failed: The value that #each loops over must be an Array. You passed <Ember.Deferred:ember285> ember-1.3.1.js:3285

Uncaught TypeError: Object [object Object] has no method 'addArrayObserver' 

请帮忙。感谢...

1 个答案:

答案 0 :(得分:1)

如果你从模型钩子返回它,它会正常工作。如果你从某个计算属性返回它,你应该只返回问题。

 var dfd = Ember.Deferred.create();
 var issues = Em.A();

 $.getJSON( App._api_path +  "issues/" + mag_id , 
   function(response){

    response.forEach(function(m){
       issues.pushObject(App.Issue.create(m));
        });

    dfd.resolve(issues);
});

return issues;