/web/js/modules/store-clients/collections/customers.js
https://github.com/seosamba/ecommerce · JavaScript · 83 lines · 81 code · 2 blank · 0 comment · 10 complexity · f0a137fcb38c406b8b9f5bdc022067e7 MD5 · raw file
- define([
- 'backbone',
- '../models/customer_row'
- ], function(Backbone, CustomerRowModel){
- var CustomersCollection = Backbone.Collection.extend({
- model: CustomerRowModel,
- urlRoot: 'api/store/customers/',
- paginator: {
- limit: 30,
- offset: 0,
- last: false
- },
- order: {
- by: 'reg_date',
- asc: false
- },
- searchTerm: '',
- roleId: '',
- cached: {},
- clientsFilter: 'clients-only',
- initialize: function(){
- this.bind('reset', this.updatePaginator, this);
- },
- url: function(){
- var url = this.urlRoot + 'for/dashboard/',
- order = '';
- url += '?'+'limit='+this.paginator.limit+'&offset='+this.paginator.offset+'&clientsFilter='+this.clientsFilter;
- if (this.order.by) {
- url += '&order=' + this.order.by + ' ' + (this.order.asc ? 'asc' : 'desc');
- }
- if (this.searchTerm) {
- url += '&search='+this.searchTerm;
- }
- if (this.roleId) {
- url += '&roleId='+this.roleId;
- }
- return $('#website_url').val() + url + '&id=';
- },
- next: function(callback) {
- if (!this.paginator.last) {
- this.paginator.offset += this.paginator.limit;
- return this.fetch().done(callback);
- }
- },
- previous: function(callback) {
- if (this.paginator.offset >= this.paginator.limit){
- this.paginator.offset -= this.paginator.limit;
- return this.fetch().done(callback);
- }
- },
- updatePaginator: function(collection) {
- if (this.length === 0){
- this.previous();
- } else {
- this.paginator.last = (this.length < this.paginator.limit);
- }
- },
- checked: function(){
- return this.filter(function(customer){ return customer.has('checked') && customer.get('checked'); });
- },
- search: function(term){
- if (term !== this.searchTerm){
- this.searchTerm = escape(term);
- this.paginator.offset = 0;
- this.paginator.last = false;
- this.paginator.order = {by: null,asc: true};
- return this.fetch();
- }
- },
- clientsFilterAction: function (term) {
- if (term !== this.clientsFilter){
- this.clientsFilter = escape(term);
- this.paginator.offset = 0;
- this.paginator.last = false;
- this.paginator.order = {by: null,asc: true};
- return this.fetch();
- }
- }
- });
- return CustomersCollection;
- });