adsys /pub/js/vendors/backbone/backbone-pcollection.js

Language Javascript Lines 105
MD5 Hash 0aca319576c514bfb8165f00e42c27d6
Repository https://github.com/oliver96/adsys.git View Raw File View Project SPDX
| Open JSFiddle
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
Backbone.PaginatedCollection = (function ( Backbone, _, $ ) {
    'use strict';
    
    var PaginatedCollection = Backbone.Collection.extend({
        pageInfo: {
            totalPage: 0
            , page: 1
            , pageSize: 10
            , showFirst: true
            , showLast: true
            , showInfo: true
        }
        , queryOptions: {}
        , responseInfo: {}
        , sync: function ( method, model, options ) {
            if(!options.data) options.data = {};
            _.extend(options.data, this.pageInfo);
            this.queryOptions = options.data;
            options.type = 'POST'; // post进行提交
            return Backbone.sync.apply(this, [method, model, options]);
        }
        , parse: function(models, options) {
            if(this.model.localStorage || this.localStorage) {
                this.pageInfo.totalPage = Math.ceil(models.length / this.pageInfo.pageSize);
                if(this.pageInfo.totalPage < 0) {
                    this.pageInfo.totalPage = 0;
                    this.pageInfo.page = 1;
                }
                if(this.pageInfo.totalPage > 0 && this.pageInfo.page > this.pageInfo.totalPage) {
                    this.pageInfo.page = this.pageInfo.totalPage;
                }
                var tmpModels = [],
                    start = (this.pageInfo.page - 1) * this.pageInfo.pageSize,
                    end = this.pageInfo.pageSize + start;
                if(models.length < end) {
                    end = models.length;
                }
                
                for(var i = start; i < end; i ++) {
                    if(typeof models[i] != 'undefined') {
                        models[i]['__no__'] = i + 1;
                        tmpModels.push(models[i]);
                    }
                }
                
                return tmpModels;
            }
            else {
                this.pageInfo.totalPage = models.totalPage;
                for(var key in models) {
                    if(key == 'data') continue;
                    this.responseInfo[key] = models[key];
                }
                if(models.data) {
                    return models.data;
                }
                else {
                    return models;
                }
            }
        }
        , getInfo: function(key) {
            if(typeof this.responseInfo[key] != 'undefined') {
                return this.responseInfo[key];
            }
            return null;
        }
        , setPage: function(page) {
            this.pageInfo.page = page;
        }
        , firstPage: function() {
            this.setPage(1);
            this.fetch({data: this.queryOptions});
        }
        , lastPage: function() {
            this.setPage(this.pageInfo.totalPage);
            this.fetch({data: this.queryOptions});
        }
        , changePage: function(page) {
            if(page > 0 && page <= this.pageInfo.totalPage) {
                this.setPage(page);
                this.fetch({data: this.queryOptions});
            }
        }
        , prevPage: function() {
            var page = this.pageInfo.page - 1;
            if(page < 1) {
                page = 1;
            }
            this.setPage(page);
            this.fetch({data: this.queryOptions});
        }
        , nextPage: function() {
            var page = this.pageInfo.page + 1;
            if(page > this.pageInfo.totalPage) {
                page = this.pageInfo.totalPage;
            }
            this.setPage(page);
            this.fetch({data: this.queryOptions});
        }
    });
    
    return PaginatedCollection
}( Backbone, _, jQuery ));
Back to Top