Javascript 从路线过渡不需要';t删除已退出的路由';s控制器观察员在Ember.js中
这里有一个JSBin来说明我的问题 我正在使用依赖项注入为API创建轮询机制,类似这样Javascript 从路线过渡不需要';t删除已退出的路由';s控制器观察员在Ember.js中,javascript,ember.js,dependency-injection,observers,ember-controllers,Javascript,Ember.js,Dependency Injection,Observers,Ember Controllers,这里有一个JSBin来说明我的问题 我正在使用依赖项注入为API创建轮询机制,类似这样 App.Poller = Ember.Object.extend({ interval: function() { return 1000; }.property().readOnly(), schedule: function(f) { return Ember.run.later(this, function() { f.apply(this); t
App.Poller = Ember.Object.extend({
interval: function() {
return 1000;
}.property().readOnly(),
schedule: function(f) {
return Ember.run.later(this, function() {
f.apply(this);
this.set('timer', this.schedule(f));
}, this.get('interval'));
},
stop: function() {
this.set('running', false);
Ember.run.cancel(this.get('timer'));
},
start: function() {
if (!this.get('running')) {
this.set('running', true);
this.set('timer', this.schedule(this.get('onPoll')));
}
},
onPoll: function() {
Ember.Logger.log('basic poller overwrite with your method');
}
});
App.register('poller:main', App.Poller);
App.inject('route', 'poller', 'poller:main');
App.inject('controller', 'poller', 'poller:main');
App.ParentChild1Controller = Ember.Controller.extend({
needs: ['parent'],
progress: Ember.computed.alias('controllers.parent.progress'),
pollingChild1: function() {
progress = this.get('progress');
Ember.Logger.log('called from pollingChild1 : ', progress);
if (progress < 50) {
this.poller.start();
} else {
this.transitionToRoute('parent.child2');
}
}.observes('progress').on('init')
});
这样我就可以从路由和控制器调用开始和停止轮询
我设置了父路由,这样它就可以像这样间歇性地轮询服务器在父路由中的进度(注意,fetch语法来自Ember Data beta 12,但工作正常)
作为step过程的一部分,我有各种子路由,这取决于从API轮询接收到的数据,因此在子控制器中我设置了这样一个观察者
App.Poller = Ember.Object.extend({
interval: function() {
return 1000;
}.property().readOnly(),
schedule: function(f) {
return Ember.run.later(this, function() {
f.apply(this);
this.set('timer', this.schedule(f));
}, this.get('interval'));
},
stop: function() {
this.set('running', false);
Ember.run.cancel(this.get('timer'));
},
start: function() {
if (!this.get('running')) {
this.set('running', true);
this.set('timer', this.schedule(this.get('onPoll')));
}
},
onPoll: function() {
Ember.Logger.log('basic poller overwrite with your method');
}
});
App.register('poller:main', App.Poller);
App.inject('route', 'poller', 'poller:main');
App.inject('controller', 'poller', 'poller:main');
App.ParentChild1Controller = Ember.Controller.extend({
needs: ['parent'],
progress: Ember.computed.alias('controllers.parent.progress'),
pollingChild1: function() {
progress = this.get('progress');
Ember.Logger.log('called from pollingChild1 : ', progress);
if (progress < 50) {
this.poller.start();
} else {
this.transitionToRoute('parent.child2');
}
}.observes('progress').on('init')
});
App.ParentChild1Controller=Ember.Controller.extend({
需要:[“家长”],
进度:Ember.computed.alias('controllers.parent.progress'),
pollingChild1:函数(){
progress=this.get('progress');
log('从pollingChild1调用:',progress);
如果(进度<50){
这个.poller.start();
}否则{
这个.transitionRoute('parent.child2');
}
}.观察('progress')。在('init'上)
});
它只是开始轮询,一旦进程超过50,就转换到下一个路由
我不明白的是,为什么在过渡到新路线后,这个观察者继续被称为
如果在路由更改时从该JSBin查看控制台,它仍在被调用
非常感谢您对可能发生这种情况的任何建议。轮询器之所以继续运行,是因为控制器是单例的,这意味着控制器将在应用程序的生命周期中一直存在 在您的情况下,
parent
控制器甚至不会被触碰,因为您所做的只是转换到parent
路由的另一个chid。但是,即使您交换了父控制器的模型,轮询器仍然会运行,因为它在作为单例的控制器上运行
您可以在此处阅读更多信息: