gpt4 book ai didi

ember.js - 将模型推送到商店时更新 View

转载 作者:行者123 更新时间:2023-12-04 15:11:54 25 4
gpt4 key购买 nike

我的应用程序中有一个相当复杂的页面,其中显示了许多不同的模型。我通过 /updates 实时更新其中几个模型REST 调用。我通过了 last_request_timestamp参数,它返回自上次调用以来创建或修改的模型。

我使用 store.push(model, model_json) 将模型添加到商店.但是,模型推送后模板不会更新。如何确保商店中的模型和 View 之间存在绑定(bind)?

最佳答案

好的,我想通了。 Ember.js FAQ

Filters, on the other hand, perform a live search of all of the records in the store's cache. As soon as a new record is loaded into the store, the filter will check to see if the record matches, and if so, add it to the array of search results. If that array is displayed in a template, it will update automatically.

...

Keep in mind that records will not show up in a filter if the store doesn't know about them. You can ensure that a record is in the store by using the store's push() method.



所以在我想要实时更新的 View 的 Controller 中,我使用 filter()在商店中获取模型。
App.PostController = Ember.ObjectController.extend({
comments: function() {
var postId = this.get('id');
return this.get('store').filter('comment', function(comment) {
return comment.get('post.id') == postId;
});
}.property('comments')
});

现在,每当我 push()一个新的 Comment要存储,它会自动添加到 View 中的相应帖子中。

关于ember.js - 将模型推送到商店时更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19639739/

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