Emberjs:由于“内容”不存在,交叉控制器绑定失败 [英] Emberjs: cross-controller binding failing because 'content' doesn't exist yet

查看:99
本文介绍了Emberjs:由于“内容”不存在,交叉控制器绑定失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用需求功能,允许一个控制器从另一个控制器获取值。这是一个JSFiddle,它在绑定值之前显示了我的应用程序的精简版本: 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({
需要:['application'],
init:function(){
this._super() ;
},
// clickCountBinding:'controllers.application.clickCount'
});

基本上,我的ApplicationController有一个clickCount属性,链接被点击。点击链接还可以通过linkTo来激活DishRoute。



现在我想让含有的DishController也可以访问ApplicationController的clickCount。所以我添加'需要'属性和一个clickCountBinding属性(它必须在JSFiddle中取消注释)。然后,当我点击一个链接我得到一个投诉:
断言失败:不能将set('clickCount',0)委托给对象代理的'content'属性:它的'content'是未定义的。 >

显然,在控制器上设置模型内容之前,绑定正在被激活。由于控制器被linkTo设置,我的DishRoute.model()和DishRoute.setupController()方法不被调用。另外,DishController.init()方法在绑定错误发生之前甚至没有被调用。



我认为我应该把内容成员对象粘贴到类(在JSFiddle中注释),但这样做会产生一个奇怪的结果:点击次数为不同的链接单独递增。有趣的是,而不是我之后。



那么 - 如何在这些控制器之间共享clickCount值?有没有其他方法来设置DishController中的内容,以便绑定工作?

解决方案

你只是稍微误解了错误消息。

问题是您从 ObjectController 中子类化 ApplicationController 即使它没有底层内容对象进行代理,在这种情况下,您应该只是用户 Ember.Controller
这就是说,如果你有一个柜台,你应该可以将它默认为零。

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

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


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?

解决方案

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'
});

这篇关于Emberjs:由于“内容”不存在,交叉控制器绑定失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆