gpt4 book ai didi

ember.js - EmberData 在没有路由的 Controller 中管理模型

转载 作者:行者123 更新时间:2023-12-02 06:10:05 24 4
gpt4 key购买 nike

我正在使用 {{render "header"}} 将我的应用程序的一部分从单独的 Handlebars 模板中的主模板中分离出来。在正确的地方渲染它。
这个单独的模板也有它自己的 Controller 来显示来自模型的一些数据(App.Notification)。
我想要做的是显示有限的notifications以及新通知的总数。
当 Controller 的 notifications 时,Ember 数据从服务器加载 10 个条目。属性在每个循环中被调用,但只要我尝试通过 splice 限制显示的通知数量,它不会返回任何数据。

基本上,如果我无法在路由中设置 Controller 模型,我将无法处理模型数据,我认为这就是 slice 的正常语法的原因在这种情况下不起作用。
我已经搜索了 Stackoverflow 和 Ember 文档,但只找到了具有正常 Route -> Controller 设置的示例,但没有关于此特定主题的内容,所以问题是,如何在此设置中正确处理模型数据?

header 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}}

最佳答案

要通过命名约定在不直接与路由关联的 Controller 上设置属性,请参阅以下 jsbin。

http://emberjs.jsbin.com/gugajaki/3/edit

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)

您发布的代码中有几个问题将在链接中解决,但为了清楚起见,我将在此处列举:
  • 您可以直接在模板中使用长度属性
  • 使用 Ember.computed.filterBy 获得非常有效的数组观察
  • 使用应用程序路由的“controllerFor”方法
  • 配置您的 header Controller 的单例实例
  • 我不懂拼接,但无论如何我都不会这样做

  • 希望这可以帮助。

    关于ember.js - EmberData 在没有路由的 Controller 中管理模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23350889/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com