2

This is a pretty basic, but for some reason I can't get it working.

I've created a companies resource:

App.Router.map(function() {
    this.resource('companies');
});

and specified a model

App.Company = DS.Model.extend({
    name: DS.attr('string'),
});

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

I have some Fixtures for test data.

Now, shouldn't I have access to my companies data from the controller? Like this

App.CompaniesController = Ember.ArrayController.extend({
    length: function () {
        return this.get('length');
    }.property(),
});

I'm getting 0, and everything I've tried in this function makes me think somehow the controller isn't getting loaded with the data - even though I am able to loop through the models in my template, like this:

  <script type="text/x-handlebars" data-template-name="companies">
    <div class="row">
      <div class="span12">
        <h3>Companies</h3>
        {{#each controller}}
          <li>{{ name }}</li>
        {{/each}}
        </ul>
      </div>
    </div>
  </script>

Some parts of the docs show setupController instead of model: function() {}, but I'm not clear about the differences. I've tried both, to no success.

Any ideas?

4

2 回答 2

3

您应该声明计算属性所依赖的属性(或属性)的路径。在这种情况下,content.length

App.CompaniesController = Ember.ArrayController.extend({
    length: function () {
        return this.get('content.length');
    }.property('content.length'),
});

或使用Binding

App.CompaniesController = Ember.ArrayController.extend({
    lengthBinding: 'content.length'
});

但是计算属性比绑定(内部)需要更少的工作,并且通常更快。

于 2013-07-17T21:12:08.413 回答
1

您将“长度”声明为属性,而没有观察控制器内容的任何更改,因此,当创建控制器的实例时,它是 0,这就是它永远不会更新的原因,您可以执行 Panagiotis 的操作说或者您也可以观察内容的“@each”属性:

App.CompaniesController = Ember.ArrayController.extend({
  length: function () {
    return this.get('length');
  }.property('@each')
});
于 2013-07-17T22:20:21.943 回答