EmberJS: Unable to get the length of an hasMany array two levels down

醉酒当歌 提交于 2019-12-04 04:59:58

Here is an example of how you get access to the first object in the array of spreads:

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')
});

But i guess you want to get the total number of pages here. So, here is an example how to iterate the spreads and sum the number of pages:

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')
});

Note: Look at the property dependency i declared via property. Since the ComputedProperty depend on those paths, you need to declare them there.

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
  }

});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!