Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

作り

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'); // ○
    }
});
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away