0

我的 emberjs 应用程序中有一个没有任何数据库数据的模型。我创建了这个模型,因为我想要一些对象来汇集来自服务器的一些请求,但没有绑定到控制器或视图。

就像是: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');。有没有办法从模型中做到这一点?

第二个问题是,有时当我进入控制器时,我需要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 个,有时甚至更多的间隔来从事同一项工作。

我怎样才能停止它并只使一个功能起作用?

PS:如果您发现我需要用其他东西更改模型,我可以更改它。


tl;博士

  • 如何从控制器内部更改路由?
  • 我怎样才能让一个函数以间隔运行?
  • 还有另一种(更好的)方法吗?


编辑

我的图书馆版本:

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 
4

1 回答 1

2

第一个问题是我无法使用此模型更改路线:$this.transitionToRoute('problem');。有没有办法从模型中做到这一点?

您不能/不应该从模型访问路由器。每当您发现有必要这样做时,就会提示您编写的代码可能不应该是模型。在这种情况下Webapp.StatusHelper, a 应该真的是一个控制器。所以像:

Webapp.StatusController = Ember.Controller.extend({
  //your code here
});

然后您可以摆脱 init() App.ready() 钩子,而是使用 ApplicationRoute 的 setupController 钩子来配置和启动计时器。当然,由于您现在处于控制器中,因此您将能够访问路由器。

我怎样才能停止它并只使一个功能起作用?

所以基本上你需要在过渡到新路线时启动/停止计时器。当路线改变时,使用路线的activatedeactivatesetupController钩子打开/关闭计时器。这样,您可以确保计时器仅在您需要时运行。

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()
于 2013-04-18T01:25:01.160 回答