2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ember.js notifyPropertyChange ではまったのでメモ

Posted at

##作り

###Controller
controllerは親子にしており、子では親のプロパティのエイリアスを貼っている
(observeHoge)

controller.js
App.OyaController = Ember.Controller.extend({
    observeHoge: null
});

App.KoController = Ember.Controller.extend({
    needs: ['oya'],
    observeHoge: Ember.computed.alias('controllers.oya.observeHoge')
});

###View
子のviewで上記プロパティを参照し、observeで依存した処理を行っている。

view.js
App.KoView = Ember.View.extend({
    change: function() {
        console.log(this.get('controller.observeHoge'));
    }.observes('controller.observeHoge')
});

##やりたいこと
プロパティが変わったか?に関わらず、changeを実行したかった。

controller.js
App.KoController = Ember.Controller.extend({
    needs: ['oya'],
    observeHoge: Ember.computed.alias('controllers.oya.observeHoge'),
    actions: function(o) {
        this.set('observeHoge', o);
        // ここで、changeが毎度動いて欲しい
    }
});

##notifyPropertyChangeを使ってみた

そこで、notifyPropertyChangeを使ったが、以下の結果となった。

controller.js
App.KoController = Ember.Controller.extend({
    needs: ['oya'],
    observeHoge: Ember.computed.alias('controllers.oya.observeHoge'),
    actions: function(o) {
        this.set('observeHoge', o);
        // ここで、changeが毎度動いて欲しい
        this.notifyPropertyChange('observeHoge'); // ×
        this.notifyPropertyChange('controllers.oya.observeHoge'); // ×
        this.get('controller.oya').notifyPropertyChange('observeHoge'); // ○
    }
});
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?