可以查看其控制器内容与另一个控制器绑定的内容

时间:2013-08-09 01:45:37

标签: ember.js

我正在尝试观察视图控制器的内容。视图的控制器通过contentBinding绑定(单向)到另一个控制器的内容。我试图在内容发生变化时执行“facContent”。不行。可能是PEBCAK。任何帮助表示赞赏。

/*________VIEW OBSERVING CONTROLLER CONTENT___________*/
App.GeoView = Ember.View.extend({
facsContent: function() {
    //do something
}.observes('controller.content'),
});

/*_________CONTROLLER WHO IS BEING OBSERVED BY GEOVIEW / HAS CONTENT BINDING TO ANOTHER CONTROLLER__*/
App.GeoController = Ember.ObjectController.extend({
     content:[],
     contentBinding:('controllers.geoFacs'),
});

/*___________CONTROLLER WHOSE CONTENT CHANGES_____________*/
App.GeoFacsController = Ember.ArrayController.extend({
content:[]
});

1 个答案:

答案 0 :(得分:2)

我想问题是,如果为您的GeoFacsController实例分配了全新的内容数组,那么观察者只会触发。我想你想在每次添加或从数组中删除新项目时触发它。然后,您必须对数组使用@each表示法:

App.GeoView = Ember.View.extend({
facsContent: function() {
    //do something
}.observes('controller.content.@each'),
});

写完这篇文章后,我发现你的代码中有一点错误。您的GeoController必须在needs属性的帮助下指定它想要访问GeoFacsController。但也许这只是你的例子中的一个错字: - )

App.GeoController = Ember.ObjectController.extend({
     needs : ["geoFacs"],
     content:[],
     contentBinding:('controllers.geoFacs'),
});