【问题标题】:How to bind to a global variable in Emberjs如何绑定到 Emberjs 中的全局变量
【发布时间】:2014-04-11 10:59:14
【问题描述】:

以下似乎不起作用:

//Global variable needed for Websocket methods
var dispatcher = new WebSocketRails(document.location.host+'/websocket')
//dispatcher.state = 'connected' or 'disconnected'. This automatically updates
//  its value if connected or not. I want to bind this value to a property below.

App.MessagesController = Ember.ArrayController.extend({

  isConnectedBinding: 'dispatcher.state', //also tried dispatcher.state w/o quotes, didn't work

  isConnected: (function() {
    alert('!!!!'); //This alert never pops up
    if (this.isConnectedBinding === 'connected') {
      return true;
    } else {
      return false;
    }
  }).property('isConnectedBinding')
});

我在前端使用 Emberjs 1.5.1 的 Ruby on Rails 服务器上使用 Websocket-rails gem。

我测试了dispatcher.state 确实改变了它的值(比如我暂时关闭了我的本地服务器:'connected' -> 'disconnected')。所以我猜它必须沿着 Emberjs 绑定。我还发现使用 Chrome 控制台时,App.__container__.lookup('controller:messages').isConnected 的值始终是 undefined,而从来不是 true 也不是 false

任何帮助将不胜感激。

【问题讨论】:

  • 你找到解决方案了吗

标签: ruby-on-rails ember.js websocket


【解决方案1】:

只需从isConnectedBinding: 'dispatcher.state' 中删除绑定和单引号,因为调度程序是一个全局变量。参考这个jsbin,你会有所了解的。

【讨论】:

  • 不工作。在 Chrome 控制台上尝试过:在我暂时关闭服务器后,dispatcher.state 从“已连接”变为“已断开”,我希望 App.__container__.lookup('controller:messages').isConnected 也可以更改为“已断开”,但事实并非如此。它仍然是“连接的”。如果我没记错的话,这是因为 isConnectedBinding 是一个绑定,而 isConnected: dispatcher.state 将被视为一个普通属性,其值 = dispatcher.state 因此将始终是“已连接”值。见(this)(我编辑了你的链接)
猜你喜欢
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
  • 2012-11-09
  • 2014-10-18
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
相关资源
最近更新 更多