cms/app-client/app/routes/tabs/thematiques.js
author ymh <ymh.work@gmail.com>
Tue, 08 Nov 2016 01:22:56 +0100
changeset 394 48458e099b05
parent 338 4a3899b6a7ed
child 474 245b4df137d3
permissions -rw-r--r--
make dynamic filters for all route and do some code pruning and cleaning

import Ember from 'ember';
import _ from 'lodash/lodash';

export default Ember.Route.extend({

    player: Ember.inject.service(),
    filter: Ember.inject.service(),

    index: 0,
    limit: 40,
    sort: 'alphabetical',

    themes: [],

    model: Ember.observer('index', function() {
        var self = this;
        var filterQueryParams = _.clone(this.get('filter').get('queryParamsValues'));
        var promise = this.store.query('theme', _.merge(filterQueryParams, {
            'limit': this.get('limit'),
            'index': this.get('index'),
            'sort': this.get('sort')
        }));
        promise.then(function(value) {
            if (self.get('themes').length) {
                value = self.get('themes').pushObjects(value.get('content'));
            }
            self.set('themes', value);
        });
        return promise;
    }),

    setupController: function(controller) {
        this._super(...arguments);
        controller.set('themes', this.get('themes'));
    },

    deactivate: function () {
        this.set('themes', []);
    },

    activate: function() {
        this.get('player').set('window', false);
    },

    actions: {

        setIndexQueryparams: function() {
            this.set('index', this.get('index') + 1);
        },

        setSortQueryparams: function(sort) {
            this.set('sort', sort);
            this.get('themes').get('content').clear();
            if(this.get('index') === 0) {
                // Force property reset to trigger request
                this.propertyWillChange('index');
                this.set('index', 0);
                this.propertyDidChange('index');
            } else {
                this.set('index', 0);
            }
        }

    }

});