[英]Access actions from within an route in Ember.js
I'm updating the following route: 我正在更新以下路线:
App.SomeRoute = Ember.Route.extend({
events: {
getMore: function(){
var controller = this.get('controller'),
nextPage = controller.get('page') + 1,
perPage = controller.get('perPage'),
items;
items = this.events.fetchPage(nextPage, perPage);
controller.gotMore(items, nextPage);
},
fetchPage: function(page, perPage){
. . . .
}
}
});
For the most part, it works fine. 在大多数情况下,它工作正常。 However, the
events
syntax has been deprecated. 但是,不推荐使用
events
语法。 Instead, we're suppose to use actions
. 相反,我们假设使用
actions
。 Updating that works well enough, until I get to the line: 更新工作得很好,直到我到达该行:
items = this.events.fetchPage(nextPage, perPage);
This throws the error: 这会引发错误:
TypeError: this.events is null
Unfortunately, I get the same error if I update the code to: 不幸的是,如果我将代码更新为:
items = this.actions.fetchPage(nextPage, perPage);
=> TypeError: this.actions is null
How can I access action methods from within the same route? 如何从同一路径中访问操作方法?
You have to use .send() to call a function inside the actions hash. 您必须使用.send()来调用actions散列中的函数。 Example:
例:
App.SomeRoute = Ember.Route.extend({
actions: {
getMore: function(){
var controller = this.get('controller'),
nextPage = controller.get('page') + 1,
perPage = controller.get('perPage'),
items;
items = this.send('fetchPage', nextPage, perPage);
controller.gotMore(items, nextPage);
},
fetchPage: function(page, perPage){
.. . .
}
}
});
It's also worth noting that you could potentially move fetchPage
out of the actions
hash. 值得注意的是,您可能
fetchPage
移出actions
哈希。 If it's only used 'internally' and does not respond directly to UI events, then it doesn't need to be in the actions
hash. 如果它仅用于'内部'并且不直接响应UI事件,那么它不需要在
actions
哈希中。 Then you can just call it with : 然后你可以用它来调用它:
items = this.fetchPage(nextPage, perPage);
You can use the send(actionName, args...)
method to execute some action. 您可以使用
send(actionName, args...)
方法执行某些操作。
In your case: 在你的情况下:
this.send('fetchPage', nextPage, perPage);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.