Ember.js 无路由控制器中的数据管理模型
我正在使用Ember.js 无路由控制器中的数据管理模型,ember.js,controller,ember-data,models,Ember.js,Controller,Ember Data,Models,我正在使用{{render“header”}将我的应用程序的一部分从separate handlebar模板中的主模板中分离出来,以在正确的位置渲染它。 此separate模板还具有自己的控制器,用于显示模型中的一些数据(App.Notification)。 我要做的是显示数量有限的通知和新通知总数。 当在每个循环中调用控制器的通知属性时,Ember data从服务器加载10个条目,但只要我尝试通过拼接限制显示的通知量,它就不会返回任何数据 基本上,如果不能在路由中设置控制器模型,我在处理模型数
{{render“header”}
将我的应用程序的一部分从separate handlebar模板中的主模板中分离出来,以在正确的位置渲染它。
此separate模板还具有自己的控制器,用于显示模型中的一些数据(App.Notification
)。
我要做的是显示数量有限的通知
和新通知总数。
当在每个循环中调用控制器的通知
属性时,Ember data从服务器加载10个条目,但只要我尝试通过拼接
限制显示的通知量,它就不会返回任何数据
基本上,如果不能在路由中设置控制器模型,我在处理模型数据时会遇到问题,我假设这就是为什么在这种情况下,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}}
要通过命名约定设置与路由没有直接关联的控制器的属性,请参考以下jsbin 您发布的代码中有几个问题将在链接中解决,但为了清晰起见,我将在此列举:
希望这有帮助。谢谢您的快速回复,它真的帮助了我!虽然有些事我不明白。你说过你无论如何都不会使用拼接,但是什么是只获取部分条目的正确方法呢?余烬数据加载10个通知,使用splice仅获取前5个通知,这是迄今为止我看到的唯一方法。此外,我还需要计算控制器中的
newNotifications
,以检查是否有通知。使用hasNew:function(){返回this.get(“newNotifications”).get(“length”);}.property(“newNotifications”)
是否正确?
App = Ember.Application.create();
notifications = [
{seen: false, message: "tornado siren!!"}
{seen: true, message: "Omg lol security breach"}
{seen: false, message: "BFF Rite?"}
{seen: false, message: "steve kane 4 prez?"}
]
App.HeaderController = Ember.Controller.extend
newNotifications: Ember.computed.filterBy("notifications", "seen", false)
App.ApplicationRoute = Ember.Route.extend
setupController: (controller, model) ->
headerController = this.controllerFor("header")
#this can be a remote fetch via find. will work the same
headerController.set("notifications", notifications)