88 lines
3.0 KiB
JavaScript
88 lines
3.0 KiB
JavaScript
import Service from '@ember/service';
|
|
import { set } from '@ember/object';
|
|
import { typeOf } from '@ember/utils';
|
|
import { inject as service } from '@ember/service';
|
|
|
|
export default Service.extend({
|
|
store: service(),
|
|
message: service(),
|
|
pageSize: 32,
|
|
constraints: {},
|
|
createConstraints: null,
|
|
updateConstraints: null,
|
|
find(params) {
|
|
console.debug('Find model: ', params);
|
|
return this._getStore().find(this.get('modelName'), params);
|
|
},
|
|
list(start, limit, params) {
|
|
console.debug('List models start: ', start, ', limit: ', limit, ", search: ", params);
|
|
return this._getStore().list(this.get('modelName'), start, limit, params);
|
|
},
|
|
listPage(page, params) {
|
|
page > 0 || (page = 1);
|
|
let me = this;
|
|
let pageSize = me.get('pageSize');
|
|
return me.list((page - 1) * pageSize, pageSize, params);
|
|
},
|
|
create(model) {
|
|
console.debug('Create model: ', model);
|
|
return this._getStore().create(this.get('modelName'), model);
|
|
},
|
|
update(model, defaultPromise) {
|
|
console.debug('Update model: ', model);
|
|
let me = this;
|
|
let p = me._getStore().update(me.get('modelName'), model);
|
|
if (defaultPromise) {
|
|
p.then((m) => {
|
|
console.debug('After update: ', m);
|
|
!(model instanceof FormData) &&
|
|
Object.keys(m).forEach((prop) => {
|
|
set(model, prop, m[prop]);
|
|
});
|
|
me.get('message').alert('Updated successfully');
|
|
})
|
|
}
|
|
return p;
|
|
},
|
|
del(params) {
|
|
console.debug('Update Model: ', params);
|
|
return this._getStore().del(this.get('modelName'), params);
|
|
},
|
|
ajaxGet(path, params) {
|
|
return this._getStore().modelAjaxGet(this.get('modelName'), path, params);
|
|
},
|
|
ajaxPost(path, params) {
|
|
return this._getStore().modelAjaxPost(this.get('modelName'), path, params);
|
|
},
|
|
validate(model, constraints) {
|
|
let me = this;
|
|
console.debug('Validate model: ', model);
|
|
if (constraints) {
|
|
if (typeOf(constraints) === 'string') {
|
|
constraints = me.get(constraints);
|
|
}
|
|
else if (typeOf(constraints) !== 'object') {
|
|
validation = me.get('constraints');
|
|
}
|
|
}
|
|
else {
|
|
constraints = me.get('constraints');
|
|
}
|
|
return validate(model, constraints || me.get('constraints'));
|
|
},
|
|
createValidate(model) {
|
|
console.debug('Create validate model: ', model);
|
|
let me = this;
|
|
let constraints = me.get('createConstraints') || me.get('constraints') || null;
|
|
return constraints && validate(model, constraints);
|
|
},
|
|
updateValidate(model) {
|
|
console.debug('Update validate model: ', model);
|
|
let me = this;
|
|
let constraints = me.get('updateConstraints') || me.get('constraints') || null;
|
|
return constraints && validate(model, constraints);
|
|
},
|
|
_getStore() {
|
|
return this.get('store');
|
|
}
|
|
}); |