import ngModule from '../../module'; import ModelProxy from '../model-proxy/model-proxy'; export default class CrudModel extends ModelProxy { constructor($http, $q) { super(); this.$http = $http; this.$q = $q; this.primaryKey = 'id'; this.autoLoad = true; } get isLoading() { return this.canceler != null; } $onInit() { if (this.autoLoad) this.refresh(); } refresh(usFilter) { if (!this.url) return; let myFilter = { fields: this.fields, where: mergeWhere(this.link, this.where), include: this.include, order: this.order, limit: this.limit }; let filter = this.filter; filter = mergeFilters(myFilter, filter); filter = mergeFilters(usFilter, filter); return this.sendRequest(filter); } cancelRequest() { if (this.canceler) { this.canceler.resolve(); this.canceler = null; } } sendRequest(filter, append) { this.cancelRequest(); this.canceler = this.$q.defer(); let options = { timeout: this.canceler.promise, params: {filter: filter} }; if (this.userParams instanceof Object) Object.assign(options.params, this.userParams); return this.$http.get(this.url, options).then( json => this.onRemoteDone(json, filter, append), json => this.onRemoteError(json) ); } onRemoteDone(json, filter, append) { let data = json.data; if (append) this.orgData = this.orgData.concat(data); else this.orgData = data; this.currentFilter = filter; this.moreRows = filter.limit && data.length == filter.limit; this.onRequestEnd(); this.dataChanged(); } onRemoteError(err) { this.onRequestEnd(); throw err; } onRequestEnd() { this.canceler = null; } loadMore() { if (this.moreRows) { let filter = Object.assign({}, this.currentFilter); filter.skip = (filter.skip || 0) + filter.limit; this.sendRequest(filter, true); } } getChanges() { let create = []; let update = []; let remove = []; for (let row of this.removed) remove.push(row.$data[this.primaryKey]); for (let row of this._data) { if (row.$isNew) create.push(row.$data); else if (row.$oldData) update.push(row.$data); } let isChanged = create.length > 0 || update.length > 0 || remove.length > 0; if (!isChanged) return null; let changes = { create: create, update: update, delete: remove }; return changes; } save() { let changes = this.getChanges(); if (!changes) return this.$q.resolve(); let url = this.saveUrl ? this.saveUrl : `${this.url}/crud`; return this.$http.post(url, changes) .then(() => this.resetChanges()); } } CrudModel.$inject = ['$http', '$q']; ngModule.component('vnCrudModel', { controller: CrudModel, bindings: { orgData: ' 1 ? {and} : and[0]; } /** * Merges two loopback filters returning the merged filter. * * @param {Object} src The source filter * @param {Object} dst The destination filter * @return {Object} The result filter */ function mergeFilters(src, dst) { let res = Object.assign({}, dst); if (!src) return res; if (src.fields) res.fields = mergeFields(src.fields, res.fields); if (src.where) res.where = mergeWhere(res.where, src.where); if (src.include) res.include = src.include; if (src.order) res.order = src.order; if (src.limit) res.limit = src.limit; return res; }