define
This commit is contained in:
@@ -19,110 +19,117 @@
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
************************************************************************/
|
||||
|
||||
Espo.Collection = Backbone.Collection.extend({
|
||||
Espo.define('Collection', [], function () {
|
||||
|
||||
name: null,
|
||||
var Collection = Backbone.Collection.extend({
|
||||
|
||||
total: 0,
|
||||
name: null,
|
||||
|
||||
offset: 0,
|
||||
total: 0,
|
||||
|
||||
maxSize: 20,
|
||||
offset: 0,
|
||||
|
||||
sortBy: 'id',
|
||||
maxSize: 20,
|
||||
|
||||
asc: false,
|
||||
sortBy: 'id',
|
||||
|
||||
where: null,
|
||||
asc: false,
|
||||
|
||||
whereAdditional: null,
|
||||
where: null,
|
||||
|
||||
_user: null,
|
||||
whereAdditional: null,
|
||||
|
||||
initialize: function (models, options) {
|
||||
options = options || {};
|
||||
this.name = options.name || this.name;
|
||||
this.urlRoot = this.urlRoot || this.name;
|
||||
this.url = this.url || this.urlRoot;
|
||||
_user: null,
|
||||
|
||||
this.sortBy = options.sortBy || this.sortBy;
|
||||
this.asc = ('asc' in options) ? options.asc : this.asc;
|
||||
initialize: function (models, options) {
|
||||
options = options || {};
|
||||
this.name = options.name || this.name;
|
||||
this.urlRoot = this.urlRoot || this.name;
|
||||
this.url = this.url || this.urlRoot;
|
||||
|
||||
Backbone.Collection.prototype.initialize.call(this);
|
||||
},
|
||||
this.sortBy = options.sortBy || this.sortBy;
|
||||
this.asc = ('asc' in options) ? options.asc : this.asc;
|
||||
|
||||
Backbone.Collection.prototype.initialize.call(this);
|
||||
},
|
||||
|
||||
|
||||
sort: function (field, asc) {
|
||||
this.sortBy = field;
|
||||
this.asc = asc;
|
||||
this.fetch();
|
||||
},
|
||||
sort: function (field, asc) {
|
||||
this.sortBy = field;
|
||||
this.asc = asc;
|
||||
this.fetch();
|
||||
},
|
||||
|
||||
nextPage: function () {
|
||||
var offset = this.offset + this.maxSize;
|
||||
this.setOffset(offset);
|
||||
},
|
||||
nextPage: function () {
|
||||
var offset = this.offset + this.maxSize;
|
||||
this.setOffset(offset);
|
||||
},
|
||||
|
||||
previousPage: function () {
|
||||
var offset = this.offset - this.maxSize;
|
||||
this.setOffset(offset);
|
||||
},
|
||||
previousPage: function () {
|
||||
var offset = this.offset - this.maxSize;
|
||||
this.setOffset(offset);
|
||||
},
|
||||
|
||||
firstPage: function () {
|
||||
this.setOffset(0);
|
||||
},
|
||||
firstPage: function () {
|
||||
this.setOffset(0);
|
||||
},
|
||||
|
||||
lastPage: function () {
|
||||
var offset = this.total - this.total % this.maxSize;
|
||||
this.setOffset(offset);
|
||||
},
|
||||
lastPage: function () {
|
||||
var offset = this.total - this.total % this.maxSize;
|
||||
this.setOffset(offset);
|
||||
},
|
||||
|
||||
setOffset: function (offset) {
|
||||
if (offset < 0) {
|
||||
throw new RangeError('offset can not be less than 0');
|
||||
}
|
||||
if (offset > this.total) {
|
||||
throw new RangeError('offset can not be larger than total count');
|
||||
}
|
||||
this.offset = offset;
|
||||
this.fetch();
|
||||
},
|
||||
setOffset: function (offset) {
|
||||
if (offset < 0) {
|
||||
throw new RangeError('offset can not be less than 0');
|
||||
}
|
||||
if (offset > this.total) {
|
||||
throw new RangeError('offset can not be larger than total count');
|
||||
}
|
||||
this.offset = offset;
|
||||
this.fetch();
|
||||
},
|
||||
|
||||
parse: function (response) {
|
||||
this.total = response.total;
|
||||
return response.list;
|
||||
},
|
||||
parse: function (response) {
|
||||
this.total = response.total;
|
||||
return response.list;
|
||||
},
|
||||
|
||||
fetch: function (options) {
|
||||
var options = options || {};
|
||||
options.data = options.data || {};
|
||||
fetch: function (options) {
|
||||
var options = options || {};
|
||||
options.data = options.data || {};
|
||||
|
||||
this.offset = options.offset || this.offset;
|
||||
this.sortBy = options.sortBy || this.sortBy;
|
||||
this.asc = options.asc || this.asc;
|
||||
this.where = options.where || this.where;
|
||||
this.offset = options.offset || this.offset;
|
||||
this.sortBy = options.sortBy || this.sortBy;
|
||||
this.asc = options.asc || this.asc;
|
||||
this.where = options.where || this.where;
|
||||
|
||||
if (!('maxSize' in options)) {
|
||||
options.data.maxSize = options.more ? this.maxSize : ((this.length > this.maxSize) ? this.length : this.maxSize);
|
||||
} else {
|
||||
options.data.maxSize = options.maxSize;
|
||||
if (!('maxSize' in options)) {
|
||||
options.data.maxSize = options.more ? this.maxSize : ((this.length > this.maxSize) ? this.length : this.maxSize);
|
||||
} else {
|
||||
options.data.maxSize = options.maxSize;
|
||||
}
|
||||
|
||||
options.data.offset = options.more ? this.length : this.offset;
|
||||
options.data.sortBy = this.sortBy;
|
||||
options.data.asc = this.asc;
|
||||
options.data.where = this.getWhere();
|
||||
|
||||
return Backbone.Collection.prototype.fetch.call(this, options);
|
||||
},
|
||||
|
||||
getWhere: function () {
|
||||
return (this.where || []).concat(this.whereAdditional || []);
|
||||
},
|
||||
|
||||
getUser: function () {
|
||||
return this._user;
|
||||
}
|
||||
|
||||
options.data.offset = options.more ? this.length : this.offset;
|
||||
options.data.sortBy = this.sortBy;
|
||||
options.data.asc = this.asc;
|
||||
options.data.where = this.getWhere();
|
||||
});
|
||||
|
||||
return Backbone.Collection.prototype.fetch.call(this, options);
|
||||
},
|
||||
return Collection;
|
||||
|
||||
getWhere: function () {
|
||||
return (this.where || []).concat(this.whereAdditional || []);
|
||||
},
|
||||
|
||||
getUser: function () {
|
||||
return this._user;
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user