我正在尝试将计算属性的值从一个控制器设置为另一个控制器。
var BusinessOwner = Ember.ObjectController.extend({
actions: {
save: function(){
var self = this;
return Ember.$.ajax({
}).then(function(){
var ownerShow = self.store.getById('application',100);
ownerShow.get('ownerGeneral');
ownerShow.set('ownerGeneral', 'complete')
Ember.set(self, 'controllers.collectinfo.ownerGeneral','completed');
//self.set('controllers.collectinfo.ownerGeneral', "completed");
});
}
}
我尝试了几种不同的设置此属性但尝试失败的尝试。如果我使用自设,我必须使用Ember.set()的错误。如果我使用Ember.set()我得到错误如果没有给出obj,则collectinfo必须是全局的。
感谢您的帮助
修改 谢谢你看这个。是的我包含了需求:' collectinfo'我仍然得到Ember.set()需要用来设置对象的错误
答案 0 :(得分:1)
您还需要在控制器中提供need数组。
var BusinessOwner = Ember.ObjectController.extend({
needs: 'collectinfo'
actions: {
save: function(){
var self = this;
return Ember.$.ajax({
}).then(function(){
var ownerShow = self.store.getById('application',100);
ownerShow.get('ownerGeneral');
ownerShow.set('ownerGeneral', 'complete')
Ember.set(self, 'controllers.collectinfo.ownerGeneral','completed');
//self.set('controllers.collectinfo.ownerGeneral', "completed");
});
}
}
编码明智我建议您为要从其他控制器访问的属性创建自己的计算属性。所以代码变成这样。
var BusinessOwner = Ember.ObjectController.extend({
needs: 'collectinfo',
ownerGeneral: Ember.computed.alias('controllers.collectinfo.ownerGeneral')
actions: {
save: function(){
var self = this;
return Ember.$.ajax({
}).then(function(){
var ownerShow = self.store.getById('application',100);
ownerShow.get('ownerGeneral');
ownerShow.set('ownerGeneral', 'complete')
Ember.set(self, 'ownerGeneral','completed');
//self.set('controllers.collectinfo.ownerGeneral', "completed");
});
}
}
答案 1 :(得分:1)
您可以使用控制器需要属性设置控制器之间的依赖关系,并将其记录在Ember Guide。
App.IndexController = Em.Controller.extend({
needs: 'application',
message: 'hi!',
actions: {
changeApplicationMessage: function() {
this.set('controllers.application.message', 'good bye');
},
changeMessage: function(){
this.set('message', 'bye');
}
}
});
依赖控制器属性可以在{{controllers.controllerName.propertyName}}的控制器中访问
答案 2 :(得分:0)
我假设您的控制器声明一个needs
属性,其中“collectInfo”为值?然后它应该这样工作:
var BusinessOwner = Ember.ObjectController.extend({
needs : ['collectInfo'],
actions: {
save: function(){
var collectinfoController = this.get('controllers.collectinfo');
return Ember.$.ajax({
}).then(function(){
var ownerShow = self.store.getById('application',100);
ownerShow.get('ownerGeneral');
ownerShow.set('ownerGeneral', 'complete')
collectinfoController.set('ownerGeneral','completed');
});
}
}
答案 3 :(得分:0)
除了其他人所说的“需求”之外,只需为set和get声明一个快捷变量:
var get = Ember.get;
var set = Ember.set;
然后像这样使用它们:
set(object, 'property', 'value-to-set-property-to');