我的 emberjs 应用程序中有一个没有任何数据库数据的模型。我创建这个模型是因为我想要一些对象来汇集来自服务器的一些请求,但不绑定(bind)到 Controller 或 View 。
类似于:StatusChecker
在我的 Application.create 中我喜欢这样:
Webapp = Ember.Application.create({
rootElement: '#content',
LOG_TRANSITIONS: true,
ready: function() {
return Webapp.status_helper = Webapp.StatusHelper.create({
timer: 15
});
}
});
这将实例化一个 Webapp.StatusHelper
对象,该对象对于应用程序必须是唯一的。当我在此对象上调用 init
时,我还使用默认计时器开始轮询:
Webapp.StatusHelper = Ember.Object.extend({
init: function(params) {
this.timer = this.get("timer");
this.timeoutID = null;
this.controller = null;
this.start();
},
start: function() {
this.execute();
},
stop: function() {
clearTimeout(this.timeoutID);
},
setTimer: function(newTime) {
this.stop();
this.timer = newTime;
this.start();
},
execute: function() {
var $this = this;
this.stop();
$.get("/status").done(function(data) {
console.log(data);
if (data["status"] === 0) { // Continue trying
$this.timeoutID = setTimeout(function() {
$this.execute();
}, $this.timer * 1000);
} else if (data["status"] === 1) { // Go to a given controller
$this.setTimer(15);
$this.transitionToRoute('index'); // This doesn't work
} else {
$this.setTimer(15);
$this.transitionToRoute('problem'); // This doesn't work
}
});
}
});
第一个问题是我无法使用以下命令更改模型的路线:$this.transitionToRoute('problem');
。有没有办法从模型中做到这一点?
第二个问题是,有时当我进入 Controller 时,我需要在需要停止该计时器并以另一个间隔启动它之后立即指定 StatusHelper
的间隔,如下所示:
Webapp.IndexRoute = Ember.Route.extend
setupController: (controller) ->
Webapp.status_helper.setTimer 15
Webapp.ProblemRoute = Ember.Route.extend
setupController: (controller) ->
Webapp.status_helper.setTimer 1
发生这种情况是因为我需要每秒检查问题是否得到解决,并且索引页面会在需要时转换到问题页面。转换后,我有 2 个(有时甚至更多)时间间隔从事同一项工作。
我怎样才能停止它并只让一个功能起作用?
P.S.:如果您发现我需要用其他东西更改模型,我可以更改它。
<小时/> TL;博士- 如何从 Controller 内部更改路线?
- 如何才能让一个函数按时间间隔运行?
- 还有其他(更好)的方法吗?
我的库的版本:
DEBUG: Ember.VERSION : 1.0.0-rc.1 ember.js:348
DEBUG: Handlebars.VERSION : 1.0.0-rc.3 ember.js:348
DEBUG: jQuery.VERSION : 1.9.1
最佳答案
The first problem is that i cant change routes from the model using this: $this.transitionToRoute('problem');. is there a way to do it from model?
您不能/不应该从模型访问路由器。每当您发现有必要这样做时,这都表明您编写的代码可能不应该成为模型。在这种情况下,Webapp.StatusHelper
a 实际上应该是一个 Controller 。所以类似:
Webapp.StatusController = Ember.Controller.extend({
//your code here
});
然后你可以摆脱 init() App.ready() 钩子(Hook),而是使用 ApplicationRoute 的 setupController 钩子(Hook)来配置和启动计时器。当然,由于您现在位于 Controller 中,因此您将能够访问路由器。
how can i stop it and make only one function to work?
所以基本上你需要在转换到新路线时启动/停止计时器。使用路由的 activate
、deactivate
和 setupController
Hook 在路由更改时打开/关闭计时器。这样您就可以确保计时器仅在您需要时运行。
Webapp.IndexRoute = Ember.Route.extend
activate: (controller) ->
this.controllerFor('status').setTimer 15
this.controllerFor('status').start()
deactivate: (controller) ->
this.controllerFor('status').stop()
Webapp.IndexRoute = Ember.Route.extend
activate: (controller) ->
this.controllerFor('status').setTimer 1
this.controllerFor('status').start()
deactivate: (controller) ->
this.controllerFor('status').stop()
关于javascript - emberjs 模型中的 SetTimeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16070731/