jeudi 2 juillet 2015

Ember, run component method once when computed in controller has finished

I'd like to call method only once when computed in controller has finished.

Here is my controller:

import Ember from 'ember';

export default Ember.ArrayController.extend({

    sortProperties: ['id:desc', 'id:asc', 'value:desc', 'value:asc'],

    sortedStats: Ember.computed.sort('model', 'sortProperties'),

    actions: {
        sortBy: function(sortProperties) {
            this.set('sortProperties', [sortProperties]);
        }
    }

});

a template:

<div><button {{action 'sortBy' 'type:asc'}}>Asc</button><button {{action 'sortBy' 'type:desc'}}>Desc</button></div>

<h1>Chart</h1>

{{chart-bar width=500 height=200 series=sortedStats}}

and component:

import Ember from 'ember';

export default Ember.Component.extend({
    tagName: 'svg'
    draw: function() {
        // here I draw the chart
    },
    init: function() {
        this._super();
    },
    onChange: function(){
        console.log('onChange');
    }.observes('series.[]')
});

onChange is called as many times as model has properties to sort. In my example it has been called 6 times and model contains array with 5 properties. I'd like to call onChange when computed.sort in controller is done.

Any suggestions?




Aucun commentaire:

Enregistrer un commentaire