将项目添加到过滤后的结果从余烬数据 [英] Adding item to filtered result from ember-data

查看:97
本文介绍了将项目添加到过滤后的结果从余烬数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DS.Store ,它使用 DS.RESTAdapter 和一个 ChatMessage 对象定义如下:

I have a DS.Store which uses the DS.RESTAdapter and a ChatMessage object defined as such:

App.ChatMessage = DS.Model.extend({
    contents: DS.attr('string'),
    roomId:   DS.attr('string')
});

请注意,一个聊天消息存在于一个房间(为简单起见,未显示),所以在我的聊天消息控制器(其扩展 Ember.ArrayController )我只想加载用户当前所在的房间的消息:

Note that a chat message exists in a room (not shown for simplicity), so in my chat messages controller (which extends Ember.ArrayController) I only want to load messages for the room the user is currently in:

loadMessages: function(){ 
    var room_id = App.getPath("current_room.id");
    this.set("content", App.store.find(App.ChatMessage, {room_id: room_id}); 
}

这将内容设置为 DS.AdapterPopulatedModelArray 和我的视图愉快地显示在 {{#each}} 块中的所有返回的聊天消息。

This sets the content to a DS.AdapterPopulatedModelArray and my view happily displays all the returned chat messages in an {{#each}} block.

现在来到添加新消息,我在同一个控制器中有以下内容:

Now it comes to adding a new message, I have the following in the same controller:

postMessage: function(contents) {
    var room_id = App.getPath("current_room.id");
    App.store.createRecord(App.ChatMessage, {
        contents: contents,
        room_id: room_id
    });

    App.store.commit();
}

这将启动一个ajax请求,将消息保存在服务器上,这一切都很好,但是它不会更新视图,这很有道理,因为它是一个过滤的结果,如果我删除了$ code上的room_id过滤器> App.store.find 然后按预期更新。

This initiates an ajax request to save the message on the server, all good so far, but it doesn't update the view. This pretty much makes sense as it's a filtered result and if I remove the room_id filter on App.store.find then it updates as expected.

尝试 this.pushObject(message),从 App.store.createRecord返回的消息记录引发错误。

如何手动将项目添加到结果中? DS.AdapterPopulatedModelArray 和 DS.FilteredModelArray 中似乎没有一种方法,是不可变的。

How do I manually add the item to the results? There doesn't seem to be a way as far as I can tell as both DS.AdapterPopulatedModelArray and DS.FilteredModelArray are immutable.

推荐答案

所以几个想法:

https://github.com/emberjs/data/issues/190

如何在数据存储中收听新记录

正常的Model.find( )/ findQuery()将返回一个AdapterPopulatedModelArray,但该数组将独立存在...它不会知道任何新的已经加载到数据库中

a normal Model.find()/findQuery() will return you an AdapterPopulatedModelArray, but that array will stand on its own... it wont know that anything new has been loaded into the database

a没有params(或store.findAll())的Model.find()将返回所有记录一个FilteredModelArray,并且ember-data将注册到列表中,并且加载到数据库中的任何新记录将被添加到此数组。

a Model.find() with no params (or store.findAll()) will return you ALL records a FilteredModelArray, and ember-data will "register" it into a list, and any new records loaded into the database will be added to this array.

调用Model.filter(func)将返回一个FilteredModelArray,其中也在商店注册...,商店中的任何新记录将导致ember数据updateModelArrays,这意味着它将使用新记录调用过滤器函数,如果返回 true 那么它会把它粘在你现有的数组中。

calling Model.filter(func) will give you back a FilteredModelArray, which is also registered with the store... and any new records in the store will cause ember-data to "updateModelArrays", meaning it will call your filter function with the new record, and if you return true, then it will stick it into your existing array.

我结束了什么:创建商店后立即,我打电话给store.findAll()我回到一个类型的所有模型的数组...我附加到商店...然后在代码中的任何其他地方,我可以addArrayObservers到这些列表..像:

SO WHAT I ENDED UP DOING: was immediately after creating the store, I call store.findAll(), which gives me back an array of all models for a type... and I attach that to the store... then anywhere else in the code, I can addArrayObservers to those lists.. something like:

App.MyModel = DS.Model.extend()
App.store = DS.Store.create()
App.store.allMyModels = App.store.findAll(App.MyModel)

//some other place in the app... a list controller perhaps
App.store.allMyModels.addArrayObserver({
   arrayWillChange: function(arr, start, removeCount, addCount) {}
   arrayDidChange: function(arr, start, removeCount, addCount) {}
})

如何将模型推入其中一个不可变数组:

首先要注意:所有Ember-Data Model实例(记录)都有一个clientId属性...它是唯一的整数,用于标识数据存储区缓存中的模型,无论其是否具有实际服务器标识(示例:在做一个Model.createRecord之后)

First to note: all Ember-Data Model instances (records) have a clientId property... which is a unique integer that identifies the model in the datastore cache whether or not it has a real server-id yet (example: right after doing a Model.createRecord).

所以AdapterPopulatedModelArray本身有一个content属性...它是这些clientId的数组...当您迭代AdapterPopulatedModelArray时,迭代器循环这些clientId的手和你回来的映射到每个clientId的完整的模型实例(记录)。

so the AdapterPopulatedModelArray itself has a "content" property... which is an array of these clientId's... and when you iterate over the AdapterPopulatedModelArray, the iterator loops over these clientId's and hands you back the full model instances (records) that map to each clientId.

所以我已经完成
(这并不意味着它是正确的!)是观察那些findAll数组,并将新的clientId推送到AdapterPopulatedModelArray的内容属性... SOMETHING LIKE:

SO WHAT I HAVE DONE (this doesn't mean it's "right"!) is to watch those findAll arrays, and push new clientId's into the content property of the AdapterPopulatedModelArray... SOMETHING LIKE:

arrayDidChange:function(arr, start, removeCount, addCount){
    if (addCount == 0) {return;} //only care about adds right now... not removes...
        arr.slice(start, start+addCount).forEach(function(item) {
            //push clientId of this item into AdapterPopulatedModelArray content list
            self.getPath('list.content').pushObject(item.get('clientId'));
        });
    }

我可以说的是:为我工作:)打破下一个ember数据更新?完全可能

what I can say is: "its working for me" :) will it break on the next ember-data update? totally possible

这篇关于将项目添加到过滤后的结果从余烬数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆