87 lines
3.0 KiB
JavaScript
87 lines
3.0 KiB
JavaScript
import Ember from 'ember';
|
|
import $ from 'jquery'
|
|
|
|
export default Ember.Mixin.create({
|
|
store: Ember.inject.service(),
|
|
message: Ember.inject.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) => {
|
|
Ember.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 ($.type(constraints) === 'string') {
|
|
constraints = me.get(constraints);
|
|
}
|
|
else if ($.type(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');
|
|
}
|
|
});
|