cms/app-client/app/routes/application.js
author ymh <ymh.work@gmail.com>
Fri, 04 Nov 2016 19:03:25 +0100
changeset 392 4fbe94af93e8
parent 344 70451a4dc9ae
child 394 48458e099b05
permissions -rw-r--r--
Improve filter service. Centralize filter management in a single point
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42
7d091abf82fd add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff changeset
     1
import Ember from 'ember';
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
     2
import _ from 'lodash/lodash';
42
7d091abf82fd add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff changeset
     3
7d091abf82fd add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff changeset
     4
export default Ember.Route.extend({
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
     5
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
     6
    filter: Ember.inject.service(),
344
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
     7
    page: 1,
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
     8
    limit: 10,
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
     9
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    10
    documents: [],
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    11
    model: Ember.observer('page', function() {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    12
        var self = this;
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    13
        var promise = this.store.query('document', {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    14
            page: this.get('page'),
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    15
            perpage: this.get('limit')
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    16
        });
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    17
        promise.then(function(value) {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    18
            if(self.controller) {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    19
                self.controller.set('page', self.get('page'));
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    20
                self.controller.set('documents', self.get('documents'));
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    21
            }
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    22
            self.set('documents', value);
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    23
        });
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    24
        return promise;
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    25
    }),
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    26
344
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    27
    setupController: function(controller) {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    28
        this._super(...arguments);
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    29
        controller.set('page', this.get('page'));
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    30
        controller.set('limit', this.get('limit'));
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    31
        controller.set('documents', this.get('documents'));
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    32
    },
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    33
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    34
    /**
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    35
      Serializes value of the query parameter based on defaultValueType
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    36
      @method serializeQueryParam
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    37
      @param {Object} value
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    38
      @param {String} urlKey
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    39
      @param {String} defaultValueType
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    40
      @private
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    41
    */
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    42
    serializeQueryParam(value, urlKey, defaultValueType) {
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    43
        if(_.contains(this.get('filter').get('queryParams'), urlKey)) {
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    44
            return this.get('filter').serializeQueryParam(value, urlKey, defaultValueType);
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    45
        }
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    46
        return this._super(value, urlKey, defaultValueType);
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    47
    },
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    48
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    49
    /**
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    50
      Deserializes value of the query parameter based on defaultValueType
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    51
      @method deserializeQueryParam
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    52
      @param {Object} value
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    53
      @param {String} urlKey
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    54
      @param {String} defaultValueType
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    55
      @private
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    56
    */
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    57
    deserializeQueryParam(value, urlKey, defaultValueType) {
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    58
        if(_.contains(this.get('filter').get('queryParams'), urlKey)) {
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    59
            return this.get('filter').deserializeQueryParam(value, urlKey, defaultValueType);
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    60
        }
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    61
        return this._super(value, urlKey, defaultValueType);
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    62
    },
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    63
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    64
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    65
    actions: {
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    66
344
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    67
        setPageQueryparams: function(type) {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    68
            var page = this.get('page');
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    69
            if(type === 'previous') {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    70
                page = page - 1;
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    71
            } else if(type === 'next') {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    72
                page = page + 1;
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    73
            }
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    74
            this.propertyWillChange('page');
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    75
            this.set('page', page);
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    76
            this.propertyDidChange('page');
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    77
        },
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    78
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    79
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    80
        willTransition: function() {
199
b7c691c6179d Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    81
            // Prevent navigation from removing query parameters
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    82
            this.transitionTo({ queryParams: this.controller.get('queryParams') });
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    83
        },
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    84
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    85
        didTransition: function() {
199
b7c691c6179d Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    86
            // Append body classname depending on the route
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    87
            Ember.$('body').removeClass((this.controller.get('currentPath') || '').replace(/\//g, '-').dasherize());
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    88
            Ember.run.once(this, function() {
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    89
                Ember.$('body').addClass((this.controller.get('currentPath') ||'').replace(/\//g, '-').dasherize());
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    90
            });
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    91
        }
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    92
81
848e4a5ad4d9 update date params to be an array of date instead of a string
nowmad@nowmads-macbook-pro.local
parents: 74
diff changeset
    93
    }
848e4a5ad4d9 update date params to be an array of date instead of a string
nowmad@nowmads-macbook-pro.local
parents: 74
diff changeset
    94
42
7d091abf82fd add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff changeset
    95
});