本文介绍了观察Ember数据存储在组件中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
导出默认值Ember.Component.extend({
存储:Ember.inject.service(),
addRecord(account){
this.get('store')。createRecord('update' {
authUid:account.get('authUid'),
service:account.get('platform')
});
}
});
我有另一个组件需要观察对特定模型所做的更改(例如,添加记录或删除),并显示在该组件中。
导出默认值Ember.Component.extend({
store:Ember .inject.service(),
observeStoreChanges:/ *我应该写什么,以便每次`addRecord`
推送存储中的记录时,在这个组件中执行一个函数* /
});
解决方案
如果您是观察者模式的粉丝:
// store.js
导出默认DS.Store.extend(Ember.Evented,{
createRecord(){
const record = this._super.apply(this,arguments);
this.trigger('recordCreated',record);
return record;
}
});
// component.js
导出默认值Ember.Component.extend({
observesStoreChanges:function(record){
} .on(' store.recordCreated')
});
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*/
});
解决方案
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')
});
这篇关于观察Ember数据存储在组件中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!