0

I am trying to work out the best method of observing a property on another view instance within Ember. Currently I have the following code that does not seem to be working correctly. Looking at documentation I can't seem to find anything on whether or not observers will only work for self.

App = Ember.Application.create();

App.ApplicationView = Ember.View.extend({
  myProperty: false,
  observerFiredCount: 0,

  testObserver: function() {
    var count = this.get('observerFiredCount');
    this.set('observerFiredCount', count + 1);
  }.observes('myProperty'),

  buttonClick: function(event) {
    if(this.get('myProperty')) {
      this.set('myProperty', false);
    } else {
      this.set('myProperty', true);
    }
  },

  willInsertElement: function() {
    var button = this.$('a');
    button.click($.proxy(this.buttonClick, this));
  }
});

App.TestObserverView = Ember.View.extend({
  testObserverAcrossViews: function() {
    console.log('hello world');
  }.observes('App.ApplicationView.myProperty')
});

I guess theres 2 questions that would be helpful here.

1. Is it possible to observe a property on another object instance.

2. What path do I use to access a childview instance?

Any feedback would be greatly appreciated. I feel as though there could be a cleaner way to do this :)

4

1 回答 1

1

你所有问题的答案是你从错误的角度处理问题。视图被破坏。他们是短暂的。它们不存在于全球范围内。你需要一个控制器。控制器的寿命很长,并且注定要受到约束。与其尝试观察视图,不如观察控制器。

因此,当视图上发生单击事件时,请在控制器上设置一个属性。应用程序的其他部分可以绑定到控制器上的该属性。

于 2013-02-01T08:46:09.487 回答