jeudi 28 avril 2016

Ember js 2 counts related field (hasMany)

I feel confused with the access to ember model in the controller using a compute property.

My issue: I’m trying to create two properties one to count the related objects of hasMany field marcanet, and second limit the size of this property to only display n elements.

First scenery computes property on Model:

import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { hasMany } from 'ember-data/relationships';
import Ember from 'ember';

export default Model.extend({
    client: attr('string'),
    figure: attr('string'),
    marcanet: hasMany('marcanet'),
    title: attr('string'),
    tm_type: attr('string'),

    total: Ember.computed('marcanet.length') {
        return this.get('marcanet.length');
    }.property('model')
    // issue i can't render it on my template as 
    // so, I can't understand if it is computed right.
});

Second scenery moving to the Controller.

Here I only can access to the main object Trademark

import Ember from 'ember';

export default Ember.Controller.extend({
    // It works
    sliceTrademarks: function() {
        return this.get('model').slice(0, 5);
    }.property('model'),
    // It works
    totalTrademarks: function() {
        return this.get('model.length');
    }.property('model'),

    // It throws me an error TypeError: this.get(...) is undefined
    sliceMarcanet: function() {
        return this.get('model.marcanet').slice(0, 2);
    }.property('model'),
    // It only give me an empty result
    totalMarcanet: function() {
        return this.get('model.marcanet.length');
    }.property('model')
});




Aucun commentaire:

Enregistrer un commentaire