Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js: data-binding between controllers

I'm trying to bind in the ApplicationController to the IndexController. Here is my jsfiddle. To summarize, here is the binding I have in the Application controller

indexIsClickedBinding: "App.indexController.isClicked",

isIndexClicked: function() {
    alert("its changed to " + this.get("indexIsClicked") ) ;
}.observes('indexIsClicked'), 

So, when the isClicked value in the IndexController is changed, but the indexIsClickedBinding doesn't do anything. Any suggestions whats wrong in my code ?

Cheers

like image 602
Jeanluca Scaljeri Avatar asked Feb 07 '13 20:02

Jeanluca Scaljeri


1 Answers

Whilst the solution is simple, let me try and explain it. Please feel free to ask any questions if you're unsure.

You've bound to a property on an instance of App.indexController. However, this instance doesn't exist, because Ember.JS no longer places the instance of the controllers directly on App.

It used to reside on App in previous versions of Ember, but not any more, because it's bad practice to reference a controller using its absolute name, since it creates tight coupling between the controllers.

That's why Ember.JS have other ways to reference to controllers. It is a common requirement to need other controllers in your current controller. Ember.JS have the following methods:

  • this.controllerFor() in Ember.Route: http://emberjs.com/guides/routing/setting-up-a-controller/
  • However, controllerFor has now been deprecated in favour of:
  • needs which tells the current controller which controllers it needs access to: https://github.com/emberjs/ember.js/pull/1731

Therefore, if we implement the needs, then your controller now has access to the other controller, as well as the properties it possesses: http://jsfiddle.net/uB5tX/7/

By using the needs, we have loosely coupled the controllers. We have referenced it by its name, and Ember.JS will look-up the controller for us. If we change App then we won't need to change anything, and if/when Ember.JS changes the behaviour of controllers, we shouldn't need to update our needs again, because when the behaviour changes, then the needs will also update, bringing back the correct controller.

like image 113
Wildhoney Avatar answered Nov 02 '22 15:11

Wildhoney