1

我会得到我的领域的记录children。这里的代码:

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

App.List = DS.Model.extend({
    name: DS.attr('string'),
    children: DS.hasMany('App.User'),

    online: function() {
        var users = this.get("children");
        return users.reduce(0, function(previousValue, user){  // no record founds
            return previousValue + user.get("online");
        });
    }.property("children.@each.online")

});

App.List.find(1).get('online')没有返回记录。(由于某种原因,我无法指定App.List.children包含许多类型的记录App.Users作为嵌入记录)。

这是小提琴:JSBIN及其输出 如何解决我的问题?

4

1 回答 1

1

在 Adapter 映射上定义嵌入式模型:

App.List = DS.Model.extend({
  name: DS.attr('string'),
  users: DS.hasMany('App.User'), //use "users" as the property name to mantain ember's naming conventions
  ...
});

App.Adapter = DS.RESTAdapter.extend();

App.Adapter.map('App.List', {
  users: {embedded: 'always'} //you can use `always` or `load` which is lazy loading.
});

App.Store = DS.Store.extend({
  revision: 12,
  adapter:  App.Adapter.create()
});

希望能帮助到你

于 2013-05-12T22:01:43.277 回答