#760 autoSave
This commit is contained in:
parent
a2db6201aa
commit
961f4a30cc
|
@ -133,6 +133,8 @@ export default class ArrayModel extends ModelProxy {
|
||||||
if (this.getChanges())
|
if (this.getChanges())
|
||||||
this.orgData = this.data;
|
this.orgData = this.data;
|
||||||
|
|
||||||
|
super.save();
|
||||||
|
|
||||||
return this.$q.resolve();
|
return this.$q.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,7 +154,7 @@ export default class ArrayModel extends ModelProxy {
|
||||||
|
|
||||||
let aType = typeof a;
|
let aType = typeof a;
|
||||||
|
|
||||||
if (aType === typeof b) {
|
if (aType === typeof b)
|
||||||
switch (aType) {
|
switch (aType) {
|
||||||
case 'string':
|
case 'string':
|
||||||
return a.localeCompare(b);
|
return a.localeCompare(b);
|
||||||
|
@ -164,7 +166,6 @@ export default class ArrayModel extends ModelProxy {
|
||||||
if (a instanceof Date && b instanceof Date)
|
if (a instanceof Date && b instanceof Date)
|
||||||
return a.getTime() - b.getTime();
|
return a.getTime() - b.getTime();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (a === undefined)
|
if (a === undefined)
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -182,12 +183,11 @@ export default class ArrayModel extends ModelProxy {
|
||||||
let mergedWhere = [];
|
let mergedWhere = [];
|
||||||
let wheres = [dst.where, src.where];
|
let wheres = [dst.where, src.where];
|
||||||
|
|
||||||
for (let where of wheres) {
|
for (let where of wheres)
|
||||||
if (Array.isArray(where))
|
if (Array.isArray(where))
|
||||||
mergedWhere = mergedWhere.concat(where);
|
mergedWhere = mergedWhere.concat(where);
|
||||||
else if (where)
|
else if (where)
|
||||||
mergedWhere.push(where);
|
mergedWhere.push(where);
|
||||||
}
|
|
||||||
|
|
||||||
switch (mergedWhere.length) {
|
switch (mergedWhere.length) {
|
||||||
case 0:
|
case 0:
|
||||||
|
@ -220,6 +220,7 @@ ngModule.component('vnArrayModel', {
|
||||||
link: '<?',
|
link: '<?',
|
||||||
order: '@?',
|
order: '@?',
|
||||||
limit: '<?',
|
limit: '<?',
|
||||||
autoLoad: '<?'
|
autoLoad: '<?',
|
||||||
|
autoSave: '<?'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -11,7 +11,9 @@ export class DataModel extends Component {
|
||||||
/**
|
/**
|
||||||
* @type {Array<Object>} A JavaScript array with the model data.
|
* @type {Array<Object>} A JavaScript array with the model data.
|
||||||
*/
|
*/
|
||||||
get data() {}
|
get data() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Refresh the model data.
|
* Refresh the model data.
|
||||||
|
@ -43,11 +45,13 @@ ngModule.component('vnDataModel', {
|
||||||
* It internally uses the JavaScript Proxy to track changes
|
* It internally uses the JavaScript Proxy to track changes
|
||||||
* made in model rows.
|
* made in model rows.
|
||||||
*
|
*
|
||||||
|
* @property {Boolean} autoSave Whether to save data automatically when model is modified
|
||||||
* @event dataChange Emitted when data property changes
|
* @event dataChange Emitted when data property changes
|
||||||
* @event dataUpdate Emitted when data property changes
|
* @event dataUpdate Emitted when data property changes
|
||||||
* @event rowInsert Emitted when new row is inserted
|
* @event rowInsert Emitted when new row is inserted
|
||||||
* @event rowRemove Emitted when row is removed
|
* @event rowRemove Emitted when row is removed
|
||||||
* @event rowChange Emitted when row property is changed
|
* @event rowChange Emitted when row property is changed
|
||||||
|
* @event save Emitted when data is saved
|
||||||
*/
|
*/
|
||||||
export default class ModelProxy extends DataModel {
|
export default class ModelProxy extends DataModel {
|
||||||
constructor($element, $scope) {
|
constructor($element, $scope) {
|
||||||
|
@ -107,6 +111,8 @@ export default class ModelProxy extends DataModel {
|
||||||
this.isChanged = true;
|
this.isChanged = true;
|
||||||
this.emit('rowRemove', index);
|
this.emit('rowRemove', index);
|
||||||
this.emit('dataUpdate');
|
this.emit('dataUpdate');
|
||||||
|
if (this.autoSave)
|
||||||
|
this.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -139,16 +145,25 @@ export default class ModelProxy extends DataModel {
|
||||||
});
|
});
|
||||||
return new Proxy(obj, {
|
return new Proxy(obj, {
|
||||||
set: (obj, prop, value) => {
|
set: (obj, prop, value) => {
|
||||||
if (prop.charAt(0) !== '$' && value !== obj[prop] && !obj.$isNew) {
|
let changed = prop.charAt(0) !== '$' && value !== obj[prop] && !obj.$isNew;
|
||||||
|
if (changed) {
|
||||||
if (!obj.$oldData)
|
if (!obj.$oldData)
|
||||||
obj.$oldData = {};
|
obj.$oldData = {};
|
||||||
if (!obj.$oldData[prop])
|
if (!obj.$oldData[prop])
|
||||||
obj.$oldData[prop] = value;
|
obj.$oldData[prop] = value;
|
||||||
this.isChanged = true;
|
this.isChanged = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
let ret = Reflect.set(obj, prop, value);
|
||||||
|
|
||||||
|
if (changed) {
|
||||||
this.emit('rowChange', {obj, prop, value});
|
this.emit('rowChange', {obj, prop, value});
|
||||||
this.emit('dataUpdate');
|
this.emit('dataUpdate');
|
||||||
|
if (this.autoSave)
|
||||||
|
this.save();
|
||||||
}
|
}
|
||||||
return Reflect.set(obj, prop, value);
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -171,7 +186,7 @@ export default class ModelProxy extends DataModel {
|
||||||
let orgData = this.orgData;
|
let orgData = this.orgData;
|
||||||
if (!data) return;
|
if (!data) return;
|
||||||
|
|
||||||
for (let row of data) {
|
for (let row of data)
|
||||||
if (row.$isNew) {
|
if (row.$isNew) {
|
||||||
let orgRow = {};
|
let orgRow = {};
|
||||||
for (let prop in row)
|
for (let prop in row)
|
||||||
|
@ -183,7 +198,6 @@ export default class ModelProxy extends DataModel {
|
||||||
} else if (row.$oldData)
|
} else if (row.$oldData)
|
||||||
for (let prop in row.$oldData)
|
for (let prop in row.$oldData)
|
||||||
row.$orgRow[prop] = row[prop];
|
row.$orgRow[prop] = row[prop];
|
||||||
}
|
|
||||||
|
|
||||||
let removed = this.removed;
|
let removed = this.removed;
|
||||||
|
|
||||||
|
@ -197,6 +211,13 @@ export default class ModelProxy extends DataModel {
|
||||||
this.resetChanges();
|
this.resetChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should be implemented by child classes.
|
||||||
|
*/
|
||||||
|
save() {
|
||||||
|
this.emit('save');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Undoes all changes made to the model data.
|
* Undoes all changes made to the model data.
|
||||||
*/
|
*/
|
||||||
|
@ -290,12 +311,16 @@ export class Paginable {
|
||||||
/**
|
/**
|
||||||
* @type {Boolean} Whether the model is loading.
|
* @type {Boolean} Whether the model is loading.
|
||||||
*/
|
*/
|
||||||
get isLoading() {}
|
get isLoading() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Boolean} Whether the data is paginated and there are more rows to load.
|
* @type {Boolean} Whether the data is paginated and there are more rows to load.
|
||||||
*/
|
*/
|
||||||
get moreRows() {}
|
get moreRows() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When limit is enabled, loads the next set of rows.
|
* When limit is enabled, loads the next set of rows.
|
||||||
|
|
Loading…
Reference in New Issue