Bartheleway
Bartheleway

Reputation: 530

How to use the controller property of a view

Should I pass a variable that extend from a controller ? A string referencing the class that extend a controller ? An instance of the controller ?

If it's a subview, how to settup parent controller ?

For reference, please see the Documentation.

Upvotes: 0

Views: 55

Answers (1)

melc
melc

Reputation: 11671

The subview will automatically inherit the controller of the parent view, if however you need to associate it with an instance of another controller it is best to associate the other controller with the controller of the parent view. Example,

http://emberjs.jsbin.com/kumaxihi/2/edit

App.IndexController = Ember.Controller.extend({
  needs:["oneOther"],
  prop1:"this is prop1",
  prop2:"this is prop2",
  prop3:"this is prop3"
});

App.IndexView = Ember.View.extend({
 prop1OfController:Ember.computed.alias("controller.prop1"),
  prop2OfController:function(){
    return this.get("controller.prop2");
  }.property()

});

App.RandomSubView = Ember.View.extend({
  templateName:"random-sub-view",
  prop3OfController:Ember.computed.alias("controller.prop3"),
  thePropOfAnotherController:Ember.computed.alias("controller.controllers.oneOther.theProp")
});

App.OneOtherController = Ember.Controller.extend({
  theProp:"this is the prop of the other controller"
});

If however you want to set the controller property directly, you would do something like this.set("controller",theControllerInstance). Now the controller instance would have to be available somehow, for example either via the routing hierarchy or from association of parent controllers or even via the render function of the route (http://emberjs.com/guides/routing/rendering-a-template/).

Upvotes: 1

Related Questions