Adding item to filtered result from ember-data

前端 未结 4 659
南方客
南方客 2021-01-02 11:17

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

App.ChatMessage = DS.Model.e         


        
4条回答
  •  失恋的感觉
    2021-01-02 12:09

    so couple of thoughts:

    (reference: https://github.com/emberjs/data/issues/190)

    how to listen for new records in the datastore

    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 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.

    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.

    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) {}
    })
    

    how to push a model into one of those "immutable" arrays:

    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).

    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.

    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'));
            });
        }
    

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

提交回复
热议问题