cms/app-client/app/routes/application.js
author Chloe Laisne <chloe.laisne@gmail.com>
Mon, 17 Oct 2016 14:50:30 +0530
changeset 344 70451a4dc9ae
parent 204 dd969e178944
child 392 4fbe94af93e8
permissions -rw-r--r--
Pagination playlist
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';
7d091abf82fd add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff changeset
     2
7d091abf82fd add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff changeset
     3
export default Ember.Route.extend({
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
     4
344
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
     5
    page: 1,
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
     6
    limit: 10,
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
     7
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
     8
    documents: [],
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
     9
    model: Ember.observer('page', function() {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    10
        var self = this;
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    11
        var promise = this.store.query('document', {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    12
            page: this.get('page'),
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    13
            perpage: this.get('limit')
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    14
        });
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    15
        promise.then(function(value) {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    16
            if(self.controller) {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    17
                self.controller.set('page', self.get('page'));
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    18
                self.controller.set('documents', self.get('documents'));
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    19
            }
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    20
            self.set('documents', value);
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
        return promise;
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    23
    }),
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    24
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    25
    serializeQueryParam: function(value, urlKey) {
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    26
        if (urlKey === 'date') {
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    27
            return value;
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    28
        }
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    29
        return '' + value;
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    30
    },
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    31
344
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    32
    setupController: function(controller) {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    33
        this._super(...arguments);
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    34
        controller.set('page', this.get('page'));
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    35
        controller.set('limit', this.get('limit'));
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    36
        controller.set('documents', this.get('documents'));
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    37
    },
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    38
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    39
    deserializeQueryParam: function(value, urlKey) {
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    40
        if (urlKey === 'date') {
203
2ee21302dc47 Set application date property and filter date property observer
Chloe Laisne <chloe.laisne@gmail.com>
parents: 201
diff changeset
    41
            var array = [];
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    42
            for (var i = 0; i < value.length; i++) {
203
2ee21302dc47 Set application date property and filter date property observer
Chloe Laisne <chloe.laisne@gmail.com>
parents: 201
diff changeset
    43
                array.push(value[i]);
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    44
            }
203
2ee21302dc47 Set application date property and filter date property observer
Chloe Laisne <chloe.laisne@gmail.com>
parents: 201
diff changeset
    45
            return array;
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    46
        }
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    47
        return value;
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    48
    },
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    49
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    50
    actions: {
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    51
344
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    52
        setPageQueryparams: function(type) {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    53
            var page = this.get('page');
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    54
            if(type === 'previous') {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    55
                page = page - 1;
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    56
            } else if(type === 'next') {
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    57
                page = page + 1;
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    58
            }
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    59
            this.propertyWillChange('page');
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    60
            this.set('page', page);
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    61
            this.propertyDidChange('page');
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    62
        },
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    63
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    64
        willTransition: function() {
199
b7c691c6179d Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    65
            // Prevent navigation from removing query parameters
b7c691c6179d Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    66
            var _this = this,
b7c691c6179d Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    67
                queryParams = {};
b7c691c6179d Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    68
            this.controller.get('queryParams').map(function(parameter){
b7c691c6179d Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    69
                if(typeof parameter === 'object') {
204
dd969e178944 Fix linting, remove logs and dead code
Chloe Laisne <chloe.laisne@gmail.com>
parents: 203
diff changeset
    70
                    Object.keys(parameter).forEach(function(value) {
199
b7c691c6179d Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    71
                        queryParams[value] = _this.controller.get(value);
b7c691c6179d Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    72
                    });
b7c691c6179d Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    73
                } else {
b7c691c6179d Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    74
                    queryParams[parameter] = _this.controller.get(parameter);
b7c691c6179d Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    75
                }
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    76
            });
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    77
            this.transitionTo({ queryParams: queryParams });
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    78
        },
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    79
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    80
        didTransition: function() {
199
b7c691c6179d Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    81
            // 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
    82
            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
    83
            Ember.run.once(this, function() {
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    84
                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
    85
            });
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    86
        }
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    87
81
848e4a5ad4d9 update date params to be an array of date instead of a string
nowmad@nowmads-macbook-pro.local
parents: 74
diff changeset
    88
    }
848e4a5ad4d9 update date params to be an array of date instead of a string
nowmad@nowmads-macbook-pro.local
parents: 74
diff changeset
    89
42
7d091abf82fd add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff changeset
    90
});