1

我试图了解如何创建绑定或计算属性,即我拥有的对象数量。我可以通过以下方式获得号码(我认为):

App.MyObject.all().get("length")

当我在函数内部创建一个控制器属性时,它不会随着更多对象的下载而更新。

numOfMyObjects: function(){
  return App.MyObject.all().get("length");
}.property()

现在我在我的 ApplicationController 中有这个,但它只显示 0。

我想知道如何对所有对象执行此操作,然后还对一组过滤的对象执行此操作。

谢谢。

4

1 回答 1

1

You need to tell Ember on which properties it should observe to fire the numOfMyObjects method. For example:

numOfMyObjects: function(){
  return App.MyObject.all().get("length");
}.property('myArray.length');

However, this won't work in your case because you've got App.MyObject in your controller itself, instead you want to be instructing the appropriate route which model(s) the controller should represent.

This way you won't actually need to create a computed property, because you'll have access to the model in your Handlebars.

Please see the JSFiddle I've put together as an example: http://jsfiddle.net/ESkkb/

The main part of the code lies in the IndexRoute:

App.IndexRoute = Ember.Route.extend({
    model: function() {
        return App.Cat.find();
    }
});

We're telling the IndexController that it should represent all of the cats. And then once we've done that, we can display the cats in our view, or in our case, the number of cats:

Count: {{length}}
于 2013-02-17T01:18:29.250 回答