Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在每个循环中使用Ember.Object实例_Javascript_Loops_Ember.js_Handlebars.js - Fatal编程技术网

Javascript 如何在每个循环中使用Ember.Object实例

Javascript 如何在每个循环中使用Ember.Object实例,javascript,loops,ember.js,handlebars.js,Javascript,Loops,Ember.js,Handlebars.js,我正在组装我的第一个余烬应用程序,数据有点问题。看起来我的选择是: 通过余烬数据听起来像是这样,而且 通过一个Ember.Object进行迭代时遇到问题 通过一个简单的JS数组,我怀疑这太简单了 我猜应该使用一个Ember.Object实例: // Attempt to modify the Blog Tutorial (http://emberjs.com/guides/) to use Ember.Object for data App.Posts = Ember.Object.extend

我正在组装我的第一个余烬应用程序,数据有点问题。看起来我的选择是:

通过余烬数据听起来像是这样,而且 通过一个Ember.Object进行迭代时遇到问题 通过一个简单的JS数组,我怀疑这太简单了 我猜应该使用一个Ember.Object实例:

// Attempt to modify the Blog Tutorial (http://emberjs.com/guides/) to use Ember.Object for data
App.Posts = Ember.Object.extend([
{
    id: '1',
    title: "Rails is Omakase",
    author: { name: "d2h" },
    date: new Date('12-27-2012'),
    body: "I want this for my ORM, I want that for my template language, and let's finish it off with this routing library. Of course, you're going to have to know what you want, and you'll rarely have your horizon expanded if you always order the same thing, but there it is. It's a very popular way of consuming software.\n\nRails is not that. Rails is omakase."
}, {
    id: '2',
    title: "The Parley Letter",
    author: { name: "d2h" },
    date: new Date('12-24-2012'),
    body: "A long list of topics were raised and I took a time to ramble at large about all of them at once. Apologies for not taking the time to be more succinct, but at least each topic has a header so you can skip stuff you don't care about.\n\n### Maintainability\n\nIt's simply not true to say that I don't care about maintainability. I still work on the oldest Rails app in the world."  
}
]);
posts = App.Posts.create();
但是我在循环这些数据时遇到了麻烦:

<!-- === POSTS === -->
<script type="text/x-handlebars" id="posts">
  <table class='table'>
    <thead>
      <tr><th>Recent Posts</th></tr>
    </thead>
    {{#each model}}
      <tr><td>
        {{#link-to 'post' this}}{{title}} <small class='muted'>by {{author.name}}</small>{{/link-to}}
      </td></tr>
    {{/each}}
  </table>

  {{outlet}}
</script>
我的检查员控制台显示:

Denying load of chrome-extension://ganlifbpkcplnldliibcbegplfmcfigp/scripts/vendor/jquery/jquery.min.map. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension. (index):1
DEBUG: -------------------------------                                ember-1.0.0-rc.8.js:382
DEBUG: Ember.VERSION : 1.0.0-rc.8                                     ember-1.0.0-rc.8.js:382
DEBUG: Handlebars.VERSION : 1.0.0                                     ember-1.0.0-rc.8.js:382
DEBUG: jQuery.VERSION : 1.9.1                                         ember-1.0.0-rc.8.js:382
DEBUG: -------------------------------                                ember-1.0.0-rc.8.js:382
Assertion failed: Expected hash or Mixin instance, got [object Array] ember-1.0.0-rc.8.js:382
Assertion failed: The value that #each loops over must be an Array. You passed <App.Posts:ember284>   ember-1.0.0-rc.8.js:382
Uncaught TypeError: Object [object Object] has no method 'addArrayObserver'     ember-1.0.0-rc.8.js:21860
Ember Debugger Active
Resource interpreted as Script but transferred with MIME type text/html: "http://www.superfish.com/ws/sf_main.jsp?dlsource=tduqbwo&userId=834F74FF-83A8-4EDB-BC8A-9433A559E216".

那么,如何在Ember.Object实例中循环数据呢?

我认为您所做的是错误的。您创建了App.Posts的实例,该对象的内容是一个数组。你们应该做的是,比如说,创建App.Post的实例,并将它们放入数组中。您所做的事情是,App.Posts实例是一个对象,您将其分配给控制器的内容,因此,它只是一个对象,而不是一个数组对象,在视图中,您试图遍历它,但它不是一个数组,如果要遍历它,请尝试:

{{#each post in model.content}}
  <tr><td>
    {{#link-to 'post' this}}{{post.title}} <small class='muted'>by {{post.author.name}}</small>{{/link-to}}
  </td></tr>
{{/each}}
看看它是否有效。但也试试我说的,它更好