Emberjs: cross-controller binding failing because 'content' doesn't exist yet

六月ゝ 毕业季﹏ 提交于 2020-01-05 07:57:45

问题


I'm trying to use the 'needs' feature to allow one controller to obtain a value from another. Here's a JSFiddle that shows a stripped-down version of my app before binding a value: http://jsfiddle.net/kevinwh/WRxnE/4/

App.ApplicationController = Ember.ObjectController.extend({
  init: function() {
    this._super();
  },
  dishClicked: function() {
    console.log('clicked');
    this.incrementProperty('clickCount');
  }
});

App.DishController = Ember.ObjectController.extend({
  needs: ['application'],
  init: function() {
    this._super();
  },
  //clickCountBinding: 'controllers.application.clickCount'
});

Basically, my ApplicationController has a clickCount property that is updated (by an action) whenever one of the Dish links is clicked. Clicking on a link also activates the DishRoute via linkTo.

Now I'd like the contained DishController to also have access to ApplicationController's clickCount. So I add the 'needs' property and a clickCountBinding property (which will have to be uncommented in the JSFiddle). Then, when I click on a link I get a complaint: assertion failed: Cannot delegate set('clickCount', 0) to the 'content' property of object proxy : its 'content' is undefined.

Apparently the binding is being activated before the model content is set on the controller. Since the controller is being set up by the linkTo, my DishRoute.model() and DishRoute.setupController() methods are not invoked. Also, the DishController.init() method isn't even called before the binding error happens.

I considered the possibility that I should just stick a content member object into the class (commented out in the JSFiddle), but doing that gives a bizarre result: the click count is incremented separately for the different links. Interesting, but not what I'm after.

So - how do I share the clickCount value across these controllers? Is there some other way to set up the content in the DishController so that the binding will work?


回答1:


You've just slightly misunderstood the error message.
The issue is that you've subclassed the ApplicationController from ObjectController even though it doesn't have an underlying content object to proxy to, you should just user Ember.Controller in this case. That being said, if you have a counter you should probably default it to zero anyway.

App.ApplicationController = Ember.Controller.extend({
  clickCount: 0,
  dishClicked: function() {
    console.log('clicked');
    this.incrementProperty('clickCount');
  }
});

App.DishController = Ember.ObjectController.extend({
  needs: ['application'],
  clickCountBinding: 'controllers.application.clickCount'
});


来源:https://stackoverflow.com/questions/16329233/emberjs-cross-controller-binding-failing-because-content-doesnt-exist-yet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!