sunoceansand
sunoceansand

Reputation: 237

Counting model with ember data

I've looked at various examples on Stackoverflow but I don't seem to understand how get('length') really works.

I'm trying to get a count of users in helper model. There is probably something wrong with the way I'm trying to get('length'), but maybe I also need to use hash RSVP to get both the current model and helper model in todo route?

todo.js

export default Ember.Controller.extend({

    count: function() {
        var helper = this.get('helper');
        return helper.user.get('length');
    }.property('helper.user.[]'),

todo.js

export default DS.Model.extend({
    title: DS.attr('string'),
    isCompleted: DS.attr('boolean', {defaultValue: false}),

    list: DS.belongsTo('list', {async: true}),
    user: DS.belongsTo('user', {async: true}),

    comment: DS.hasMany('comment', {async: true}),
    helper: DS.hasMany('helper', {async: true}),
});

todo.hbs

<p>
                    <span class="badge pull-left">14 hands</span>
                    <span class="badge pull-left">{{todo.count}}</span>
                    </p>

helper.js

export default DS.Model.extend({
    user: DS.belongsTo('user', {async: true}),
    todo: DS.belongsTo('todo', {async: true}),
});

todo.js

export default Ember.Route.extend({
  model: function(params){
    // return model
  },

Upvotes: 0

Views: 149

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

User is a single object, so there probably isn't a length property on it. And with async properties you need to access them using promise notation (and a getter as well).

helper.get('user').then(function(user){
    console.log(user.get('someProperties'));
})

Upvotes: 1

Related Questions