Dinesh Kumar DJ
Dinesh Kumar DJ

Reputation: 488

Accessing properties across views

What is the efficient way to access and change properties from controllers in views? I have multiple views within views. I have to change the property of parentView without referring this.get('parentView.property'), since I reuse views often and the hierarchy changes a lot.

How to maintain variables across views effectively in ember.js?

Upvotes: 1

Views: 75

Answers (1)

thecodejack
thecodejack

Reputation: 13379

If the properties of views, are not binded as models, One way I follow is use common controller between the views and observe controller's property.

Eg.

 App.TestController = Ember.Controller.extend({
        commonProperty: "Val1"
    });

    App.View1 = Ember.View.extend({
        ObserverCommonProperty : function() {
             //runs code 
        }.observes('controller.commonProperty')
    });
    App.View2 = Ember.View.extend({
        didInsertElement: function() {
            this.get('controller').set('commonProperty',"Val2");
        }
    });

This way you will not be worried about the existence of other view objects and doesn't break any MVC rules

Upvotes: 1

Related Questions