簡體   English   中英

如何在#each循環中使用Ember.Object實例

[英]How to use Ember.Object instance with #each loop

我正在整理我的第一個Ember應用程序,但數據有些麻煩。 看來我的選擇是:

  • 通過Ember Data(類似的聲音尚未准備就緒可能已損壞
  • 通過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}}

看看是否可行。 還要嘗試我說的話,更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM