访问控制器函数内的内容 ember [英] Access content inside a controller function ember
问题描述
我有一个路由、控制器、这样的视图.问题是我从视图中调用了控制器函数 reloadTime
但在 reloadTime
函数中,我控制台了这个控制器的内容,但它说它是 undefined
.我的问题是如何在 ember 中访问此内容?
App.ActivecallsRoute = Ember.Route.extend({设置控制器:功能(控制器,模型){$.ajax({url:'requests/activecalls.php',类型:'POST',成功:功能(数据){App.Cdrglobal.set('active_call',data.length);控制器.set('内容',数据);}})}});App.ActivecallsController = Ember.ArrayController.extend({内容:[],删除调用:函数(模型){var obj = this.findProperty('ID',model.ID);App.Cdrglobal.set('active_call',App.Cdrglobal.active_call-1);this.removeObject(obj);},重新加载时间:功能(){console.log(this.get('content'));//控制台未定义console.log(this.content);控制台未定义}});App.ActivecallsView = Ember.View.extend({didInsertElement:function(){this.get('controller').reloadTime();}});
您正在正确访问 content
属性.你得到 undefined
的原因是因为 content
属性实际上是未定义的.
现在你的 content
未定义的原因是因为 Ember.js 自动将控制器的内容设置为路由中 model
钩子的返回值.>
因为你没有定义 model
方法,如果这个钩子是 undefined
的返回值,因此 Ember.js 正在设置控制器 content
属性为 undefined
.
解决方案:
创建一个只返回空数组的虚拟模型钩子:
App.ActivecallsRoute = Ember.Route.extend({设置控制器:功能(控制器,模型){$.ajax({url:'requests/activecalls.php',类型:'POST',成功:功能(数据){App.Cdrglobal.set('active_call',data.length);控制器.set('内容',数据);}});},模型:函数(){返回 [];}});
I have a route, controller, view like this. The problem is I called controller function reloadTime
from view but in reloadTime
function I console the content for this controller but it says it is undefined
. My question is how to access this content in ember?
App.ActivecallsRoute = Ember.Route.extend({
setupController:function(controller,model){
$.ajax({
url:'requests/activecalls.php',
type:'POST',
success:function(data){
App.Cdrglobal.set('active_call',data.length);
controller.set('content',data);
}
})
}
});
App.ActivecallsController = Ember.ArrayController.extend({
content:[],
deleteCall:function(model){
var obj = this.findProperty('ID',model.ID);
App.Cdrglobal.set('active_call',App.Cdrglobal.active_call-1);
this.removeObject(obj);
},
reloadTime:function(){
console.log(this.get('content'));//console undefined
console.log(this.content);console undefined
}
});
App.ActivecallsView = Ember.View.extend({
didInsertElement:function(){
this.get('controller').reloadTime();
}
});
You are accessing the content
property correctly. The reason why you are getting undefined
is because the content
property is actually undefined.
Now the reason why your content
is undefined, is because Ember.js automatically sets the controller's content to the return value of the model
hook in the route.
Since you didn't define a model
method, the return value if this hook is undefined
and therefore Ember.js is setting the controller content
property to undefined
.
Solution:
Create a dummy model hook that just returns an empty array:
App.ActivecallsRoute = Ember.Route.extend({
setupController:function(controller,model){
$.ajax({
url:'requests/activecalls.php',
type:'POST',
success:function(data){
App.Cdrglobal.set('active_call',data.length);
controller.set('content',data);
}
});
},
model: function() {
return [];
}
});
这篇关于访问控制器函数内的内容 ember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!