Ember - 观察创建/删除记录 [英] Ember — observe for creation/deletion of records

查看:98
本文介绍了Ember - 观察创建/删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两套独立的控制器/型号: Dog Cat



现在,每当我创建新的 Cat 记录(或删除现有的记录)时,我想要 Dog 控制器观察创建/删除记录,当新的 Cat 记录被创建时,我希望它触发一些动作(例如控制台.log('Bark!'))。



我不想从 Cat controller to Dog 控制器直接在这种情况下;我想要 Dog 控制器自己负责。如果有可能,是否有任何方式可以从 Dog 控制器的角度来判断是创建还是删除记录?



任何想法如何做?

解决方案

使用Ember数据



在狗/猫控制器上,您可以创建猫/狗集合的引用。



我们假设您正在创建狗控制器>

  App.DogRoute = Em.Route.extend({
model:function(){
return this.store .find('dog');
},
setupController:function(controller,model){
this._super(controller,model);
controller.set('cats ',this.store.all('cat'));
}
});

App.DogController = Em.ArrayController.extend({
catsChanged:function(){

} .observes('cats')
} );

此外,您可以添加一个数组观察器,观察项目如何更改。

  App.DogRoute = Em.Route.extend({
model:function(){
return this.store.find 'dog');
},
setupController:function(controller,model){
this._super(controller,model);
var all = this.store.all 'cat');
controller.set('cats',all);
all.addArrayObserver(controller);
}
});

App.DogController = Em.ArrayController.extend({
arrayWillChange:function(array,start,removeCount,addCount){
console.log(arguments);
},
arrayDidChange:function(array,start,removeCount,addCount){
console.log(arguments);
}
});



使用需求并观看控制器



  App.CatsController = Em.ArrayController.extend({
需要:'dog',
dogs:Em.computed.alias('controllers.dogs'),
dogsChanged:function(){
console.log('dogs changed');
} .observes('dogs。[]')
});


App.DogsController = Em.ArrayController.extend({
需要:'cat',
cats:Em.computed.alias('controllers.cats' )
catsChanged:function(){
console.log('cats changed');
} .observes('cats。[]')
});


Let's say I have 2 independent set of controllers/models: Dog and Cat.

Now, whenever I create new Cat record (or delete existing one) I want Dog controller to observe for creation/deletion of the record and when new Cat record is created I want it to fire some action (for example console.log('Bark!')).

I don't want to send action from Cat controller to Dog controller directly in this case; I want Dog controller to be responsible for itself. In case it is possible, is there any way to tell from the Dog controllers point of view whether a record was created or deleted?

Any ideas how to do that?

解决方案

Using Ember Data

On the dog/cat controller you can create a reference to the cat/dog collection.

let's assume you're creating the dog controller

App.DogRoute = Em.Route.extend({
  model: function(){
    return this.store.find('dog');
  },
  setupController: function(controller, model){
    this._super(controller,model);
    controller.set('cats', this.store.all('cat'));
  }
});

App.DogController = Em.ArrayController.extend({
  catsChanged: function(){

  }.observes('cats')
});

Additionally you could add an array observer and watch how the items are changing.

App.DogRoute = Em.Route.extend({
  model: function(){
    return this.store.find('dog');
  },
  setupController: function(controller, model){
    this._super(controller,model);
    var all = this.store.all('cat');
    controller.set('cats', all);
    all.addArrayObserver(controller);
  }
});

App.DogController = Em.ArrayController.extend({
  arrayWillChange: function(array, start, removeCount, addCount) {
    console.log(arguments);
  },
  arrayDidChange: function(array, start, removeCount, addCount) {
    console.log(arguments);
  }
});

Using needs and watching the controller

App.CatsController = Em.ArrayController.extend({
  needs: 'dog',
  dogs: Em.computed.alias('controllers.dogs'),
  dogsChanged: function(){
    console.log('dogs changed');
  }.observes('dogs.[]')
});


App.DogsController = Em.ArrayController.extend({
  needs: 'cat',
  cats: Em.computed.alias('controllers.cats'),
  catsChanged: function(){
    console.log('cats changed');
  }.observes('cats.[]')
});

这篇关于Ember - 观察创建/删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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