fabien
fabien

Reputation: 2258

Ember js : create a global object that will be obervable by every controller

Is there a way to achieve such thing ? An object like that :

App.myGlobalObject = {
    key: value,
    key2: {
        key3: value3,
        key4: value4
    }
}

And when key4 is updated, events are fired everywhere they are observed.

(bonus question : how to observe that ?)


EDIT


My first lead :

1/ Build a generix "key" object :

Key = Ember.Object.extend( {
    value: null,
    valueChange: function () {
        console.log( arguments );
    }.observes( 'value' ).on( 'change' )
} );

2/ Create an instance of this object

test = Key.create( {
    value: null
} );

3/ Ok, it works, when I try test.set('value', 'test'), the observer is triggered.

4/ Now, how do I observe it in a controller or a view ?

Upvotes: 1

Views: 45

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

You're practically there, just assign it to a global namespace (Uppercase) and then you can watch putting the fully qualified path:

App.GlobalObj = Em.Object.create({
  foo:'asdf'
});


App.IndexController = Em.Controller.extend({
  basedOnFoo: function(){
    return App.GlobalObj.get('foo') + 'modified';
  }.property('App.GlobalObj.foo')
});

Example: http://emberjs.jsbin.com/xeteb/1/edit

Upvotes: 1

Related Questions