Observe Ember Data store changes in component

孤街浪徒 提交于 2020-01-16 07:45:34

问题


I have a component which creates record for a specific model like this:

export default Ember.Component.extend({

    store: Ember.inject.service(),

    addRecord(account) {
      this.get('store').createRecord('update', {
        authUid: account.get('authUid'),
        service: account.get('platform')
      });
    }
});

I have another component that needs to observe changes done to a particular model (i.e. if records are added or deleted), and show them in that component.

export default Ember.Component.extend({
    store: Ember.inject.service(),

    observeStoreChanges: /*What should I write so that every time `addRecord`
pushes record in the store, a function is executed in this component*/

});

回答1:


If you're a fan of the observer pattern:

// store.js
export default DS.Store.extend(Ember.Evented, {
    createRecord() {
        const record = this._super.apply(this, arguments);
        this.trigger('recordCreated', record);
        return record;
    }
});

// component.js
export default Ember.Component.extend({
    observesStoreChanges: function(record) {

    }.on('store.recordCreated')
});


来源:https://stackoverflow.com/questions/30253471/observe-ember-data-store-changes-in-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!