Binding values on controllers


#1

I have two controllers, SidebarCategoriesController and MainLinksController. I have a property selectedCategory on SidebarCategoriesController and MainLinksController needs this property when rendering templates.

App.MainLinksController = Ember.ObjectController.extend({
     needs: "sidebarCategories"
});

App.SidebarCategoriesController = Ember.ObjectController.extend({
    selectedCategory:2,
    actions: {
        setCategory: function (id){
            this.set('selectedCategory',id);
        }
    }
});

I also have a templete(below) whose controller is MainLinksController:

<script  type="text/x-handlebars" data-template-name="main_links">
 <h2 class="panel-title">All links:{{controllers.sidebarCategories.selectedCategory}}</h2>
</script>

The issue is that when selectedCategory is being updated in SidebarCategoriesController, the template which is using selectedCategory from MainLinksController via {{controllers.sidebarCategories.selectedCategory}} is not getting updated. How can i make the binding so that as soon as the selectedCategory changes in SidebarCategoriesController, {{controllers.sidebarCategories.selectedCategory}} also changes in the template ?


#2

Could you replicate this in a JSBin please? Glancing at this, I think this should work as is.


#3

look at this example here: https://gist.github.com/noorbakerally/c5fe53eb645766846631 which is in jsbin here, http://jsbin.com/sihili

There are two controllers, SidebarCategoriesController and MainLinksController. I was expecting selectedCategory to be updated in MainLinksController as soon as selectedCategory gets updated in SidebarCategoriesController


#4

Seems possibly related to this http://stackoverflow.com/questions/25513056/emberjs-notifypropertychange-not-making-it-to-observer

I think this is a bug, and I’d open an issue.


#5

a workaround for this issue, http://stackoverflow.com/questions/27649027/binding-values-on-controllers/27649751?noredirect=1#comment43738368_27649751


#6

Your needs and the render function were actually generating two different instances of sidebar controller. So the alias was actually aliased to a different controller instance.

Fixed: http://jsbin.com/sojomocewu/1/edit


#7

Interesting, wouldn’t have thought that was an error prone way to send up the render.


#8

Yeah, could be a bug. I’d need to investigate. In any case, I’d open up an issue on github and feel free to tag me (@jasonmit). I’d like to dive into this one over the weekend.