【发布时间】:2014-04-28 20:47:57
【问题描述】:
我的应用程序的一部分从主模板分离到一个单独的把手模板中,使用{{render "header"}} 将其呈现在正确的位置。
这个单独的模板也有它自己的控制器来显示来自模型的一些数据 (App.Notification)。
我想要做的是显示有限数量的notifications 和新通知的总数。
当控制器的 notifications 属性在每个循环中被调用时,Ember 数据从服务器加载 10 个条目,但是一旦我尝试通过 splice 限制显示的通知数量,它就不会返回任何数据。
基本上,如果我无法在路由中设置控制器模型,我将无法处理模型数据,我认为这就是为什么 slice 的正常语法在这种情况下不起作用的原因。
我已经搜索了 Stackoverflow 和 Ember 文档,但只找到了正常 Route -> Controller 设置的示例,但没有关于这个特定主题,所以问题是,如何在这个设置中正确处理模型数据?
标题控制器:
App.HeaderController = Ember.Controller.extend
notificationNew: (->
@get('notifications').filterBy('seen', false).get('length')
).property('notifications')
notifications: (->
@store.find('notification').slice(0, 2)
).property('@each.notification')
模板:
{{#each notifications}}
...
{{/each}}
【问题讨论】:
标签: ember.js controller ember-data models