7

我试图创建一个计算属性来获取所有页面长度的总和。

但我不知道如何访问一个孩子,所以我可以得到那个孩子的孩子。

App.Document = DS.Model.extend({
    name: DS.attr('string'),
    spreads: DS.hasMany('App.Spread'),

    pagesCount: function() {
                // Here is where i go wrong, i can get the length of spreads, but not access a spread to get the page length.
                var spreadsLength = this.get('spreads.length');
                var firstSpread = this.get('spreads')[0];
                return firstSpread.get('pages.length');
    }.property('spreads')
});

App.Spread = DS.Model.extend({
    document: DS.belongsTo('App.Document'),
    pages: DS.hasMany('App.Page')
})

App.Page = DS.Model.extend({
    spread: DS.belongsTo('App.Spread'),
    page_name: DS.attr('string'),
    page_items: DS.hasMany('DS.PageItem')
})
4

2 回答 2

11

下面是一个示例,说明如何访问点差数组中的第一个对象:

App.Document = DS.Model.extend({
    name: DS.attr('string'),
    spreads: DS.hasMany('App.Spread'),

    pagesCount: function() {
        // Here is where i go wrong, i can get the length of spreads, but not access a spread to get the page length.
        var spreadsLength = this.get('spreads.length');

        var firstSpread = this.get('spreads').objectAt(0);
        // var firstSpread = this.get('spreads.firstObject'); // elegant way to first Object

        return firstSpread.get('pages.length');
    }.property('spreads.firstObject.pages.length')
});

但我想你想在这里获得总页数。因此,这是一个如何迭代点差并求和页数的示例

App.Document = DS.Model.extend({
    name: DS.attr('string'),
    spreads: DS.hasMany('App.Spread'),

    pagesCount: function() {
        // Here is where i go wrong, i can get the length of spreads, but not access a spread to get the page length.
        var spreadsLength = this.get('spreads.length');
        var ret = 0;
        this.get("spreads").forEach(function(spread)){
            ret += spread.get('pages.length');
        }
        return ret;
    }.property('spreads.@each.pages.length')
});

注意:查看我通过property. 由于 ComputedProperty 依赖于这些路径,因此您需要在此处声明它们。

于 2013-04-11T22:41:08.683 回答
-3
App.User = DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  email: DS.attr('st    App.User = DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  email: DS.attr('string'),
  phone: DS.attr('string'),
  status: DS.attr('string', { defaultValue: 'new' }),
  notes: DS.attr('string'),
//  projects: DS.hasMany("project", {async: true}),
    projectsCount: function() {
//    alert(this.get('projects'));
        return this.get('projects.length');
    }.property('id'),

  fullName: function() {
    return this.get('firstName') + ' ' + this.get('lastName')
  }.property('firstName', 'lastName')

}),

App.User.reopenClass({

  valid: function(fields) {
    return fields.firstName && fields.lastNameenter code here
  }

});ring'),
  phone: D    App.User = DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  email: DS.attr('string'),
  phone: DS.attr('string'),
  status: DS.attr('string', { defaultValue: 'new' }),
  notes: DS.attr('string'),
//  projects: DS.hasMany("project", {async: true}),
    projectsCount: function() {
//    alert(this.get('projects'));
        return this.get('projects.length');
    }.property('id'),

  fullName: function() {
    return this.get('firstName') + ' ' + this.get('lastName')
  }.property('firstName', 'lastName')

}),

App.User.reopenClass({

  valid: function(fields) {    App.User = DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  email: DS.attr('string'),
  phone: DS.attr('string'),
  status: DS.attr('string', { defaultValue: 'new' }),
  notes: DS.attr('string'),
//  projects: DS.hasMany("project", {async: true}),
    projectsCount: function() {
//    alert(this.get('projects'));
        return this.get('projects.length');
    }.property('id'),

  fullName: function() {
    return this.get('firstName') + ' ' + this.get('lastName')
  }.property('firstName', 'lastName')

}),

App.User.reopenClass({

  valid: function(fields)    App.User = DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  email: DS.attr('string'),
  phone: DS.attr('string'),
  status: DS.attr('string', { defaultValue: 'new' }),
  notes: DS.attr('string'),
//  projects: DS.hasMany("project", {async: true}),
    projectsCount: function() {
//    alert(this.get('projects'));
        return this.get('projects.length');
    }.property('id'),

  fullName: function() {
    return this.get('firstName') + ' ' + this.get('lastName')
  }.property('firstName', 'lastName')

}),

App.User.reopenClass({

  valid: function(fields) {
    return fields.firstName && fields.lastNameenter code here
  }

}); {
    return fields.firstName && fields.lastNameenter code here
  }

});
    return fields.firstName && fields.lastNameenter code here
  }

});S.attr('string'),
  status: DS.attr('string', { defaultValue: 'new' }),
  notes: DS.attr('string'),
//  projects: DS.hasMany("project", {async: true}),
    projectsCount: function() {
//    alert(this.get('projects'));
        return this.get('projects.length');
    }.property('id'),

  fullName: function() {
    return this.get('firstName') + ' ' + this.get('lastName')
  }.property('firstName', 'lastName')

}),

App.User.reopenClass({

  valid: function(fields) {
    return fields.firstName && fields.lastNameenter code here
  }

});

App.User = DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  email: DS.attr('string'),    App.User = DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  email: DS.attr('string'),
  phone: DS.attr('string'),
  status: DS.attr('string', { defaultValue: 'new' }),
  notes: DS.attr('string'),
//  projects: DS.hasMany("project", {async: true}),
    projectsCount: function() {
//    alert(this.get('projects'));
        return this.get('projects.length');
    }.property('id'),

  fullName: function() {
    return this.get('firstName') + ' ' + this.get('lastName')
  }.property('firstName', 'lastName')

}),

App.User.reopenClass({

  valid: function(fields) {
    return fields.firstName && fields.lastNameenter code here
  }

});
  phone: DS.attr('string'),
  status: DS.attr('    App.User = DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  email: DS.attr('string'),
  phone: DS.attr('string'),
  status: DS.attr('string', { defaultValue: 'new' }),
  notes: DS.attr('string'),
//  projects: DS.hasMany("project", {async: true}),
    projectsCount: function() {
//    alert(this.get('projects'));
        return this.get('projects.length');
    }.property('id'),

  fullName: function() {
    return this.get('firstName') + ' ' + this.get('lastName')
  }.property('firstName', 'lastName')

}),

App.User.reopenClass({

  valid: function(fields) {
    return fields.firstName && field    App.User = DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  email: DS.attr('string'),
  phone: DS.attr('string'),
  status: DS.attr('string', { defaultValue: 'new' }),
  notes: DS.attr('string'),
//  projects: DS.hasMany("project", {async: true}),
    projectsCount: function() {
//    alert(this.get('projects'));
        return this.get('projects.length');
    }.property('id'),

  fullName: function() {
    return this.get('firstName') + ' ' + this.get('lastName')
  }.property('firstName', 'lastName')

}),

App.User.reopenClass({

  valid: function(fields) {
    return fields.firstName && fields.lastNameenter code here
  }

});s.lastNameenter code here
  }

});string', { defaultValue: 'new' }),
  notes: DS.attr('string'),
//  projects: DS.hasMany("project", {async: true}),
    projectsCount: function() {
//    alert(this.get('projects'));
        return this.get('projects.length');
    }.property('id'),

  fullName: function() {
    return this.get('firstName') + ' ' + this.get('lastName')
  }.property('firstName', 'lastName')

}),

App.User.reopenClass({

  valid: function(fields) {
    return fields.firstName && fields.lastNameenter code here
  }

});
于 2014-07-15T09:11:53.780 回答