简体   繁体   English

如何在ember.js中将把手变量绑定到Promise

[英]How do I bind a handlebars variable to a promise in ember.js

In Ember.js I am trying to bind a handlebars conditional the outcome of a ember-data fetch that returns a promise. 在Ember.js中,我尝试绑定条件为返回承诺的余烬数据获取结果的车把。

I understand that the promise has a success call back, but I don't understand how to bind the result of that success to the value of the computed property. 我知道promise会成功调用,但是我不知道如何将成功的结果绑定到计算属性的值。 The value of the computed property always seems to be true because it returns the promise. 计算属性的值似乎总是正确的,因为它返回了promise。

{{#if game.moreThanOnePlayer}}
  <span> it was true </span>
{{/if}}


App.Game = DS.Model.extend
  players: DS.hasMany 'App.Player'
  moreThanOnePlayer: (->

    promise = @get('players') #triggers ajax call that returns a promise

    promise.then(
    (players) -> #promise success call back
      return players.length > 1 #this is what I want the computed property value to be
    )

    #but the return value of the function is still a promise, which always evaluates to true.
    return promise
  ).property('players')

ember-data version: // Last commit: ef11bff (2013-08-26 20:54:06 -0700) ember-data版本://最后提交:ef11bff(2013-08-26 20:54:06 -0700)

You should load the data in the Route and make moreThanOnePlayer a computed property. 您应该将数据加载到Route中,并使moreThanOnePlayer成为计算属性。

See http://jsbin.com/AJUWOJi/1/edit for an example. 有关示例,请参见http://jsbin.com/AJUWOJi/1/edit

I just managed to solve a very similar problem by wrapping part of the code in an Ember.run.once(...) block. 通过将部分代码包装在Ember.run.once(...)块中,我设法解决了一个非常类似的问题。 I split the property I used in handlebars and the code that watches for changes. 我拆分了我在车把中使用的属性和监视更改的代码。 In your case I would try something like: 在您的情况下,我会尝试类似的方法:

App.Game = DS.Model.extend
  players: DS.hasMany 'App.Player'
  moreThenOnePlayer: DS.attr 'boolean'
  playersUpdated: (->
    promise = @get('players')
    g = @
    promise.then(
      (players) ->
        Ember.run.once ->
          g.set 'moreThenOnePlayer', players.length > 1
    )
  ).observes('players')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM