diff --git a/@salix/core/src/core.js b/@salix/core/src/core.js index c715a7061..bba595174 100644 --- a/@salix/core/src/core.js +++ b/@salix/core/src/core.js @@ -9,6 +9,7 @@ export {NAME as INTERPOLATE, Interpolate} from './interpolate'; export {NAME as ROUTES_LOADER, RoutesLoader} from './routesLoader'; export {NAME as COPY_OBJECT} from './copy'; export {NAME as EQUALS_OBJECT} from './equals'; +export {NAME as GET_DATA_MODIFIED, factory as Modified} from './modified'; export {NAME as FOCUS, directive as Focus} from './focus'; export {NAME as RULE, directive as Rule} from './rule'; diff --git a/@salix/core/src/modified.js b/@salix/core/src/modified.js new file mode 100644 index 000000000..aceaf9a88 --- /dev/null +++ b/@salix/core/src/modified.js @@ -0,0 +1,27 @@ +import {module} from './module'; +import * as equals from './equals'; + +export const NAME = 'getDataModified'; +export function factory(equalsObject) { + return function getData(object, objectOld) { + var newObject = {}; + for (var k in object) { + var val = object[k]; + var valOld = objectOld[k]; + + if (val instanceof Object && !equalsObject(val, valOld)) { + newObject[k] = getData(val, valOld); + } + else if (val instanceof Array && val.length !== valOld.length) { + newObject[k] = val; + } + else if (val !== valOld) { + newObject[k] = val; + } + } + + return newObject; + } +} +factory.$inject = ['equalsObject']; +module.factory(NAME, factory); diff --git a/@salix/crud/src/client/basic-data/index.js b/@salix/crud/src/client/basic-data/index.js index 33ae58e41..3bdc10828 100644 --- a/@salix/crud/src/client/basic-data/index.js +++ b/@salix/crud/src/client/basic-data/index.js @@ -8,7 +8,7 @@ export const COMPONENT = { bindings: { client: '<' }, - controller: function($http, copyObject, equalsObject, $transitions, $element) { + controller: function($http, copyObject, equalsObject, $transitions, $element, modified) { var self = this; var deregister = $transitions.onStart({ }, callback); @@ -33,8 +33,10 @@ export const COMPONENT = { this.submit = function() { if (!equalsObject(this.client, this.clientOld)) { - this.client.modify = "BasicData"; - $http.put('/client/api/Clients', this.client).then( + var newClient = modified(this.client, this.clientOld); + newClient.modify = "BasicData"; + newClient.id = this.clientOld.id; + $http.put('/client/api/Clients', newClient).then( json => { copyObject(json.data, this.client); this.copyClient(); @@ -50,5 +52,5 @@ export const COMPONENT = { } }; -COMPONENT.controller.$inject = ['$http', 'copyObject', 'equalsObject', '$transitions', '$element']; +COMPONENT.controller.$inject = ['$http', 'copyObject', 'equalsObject', '$transitions', '$element', 'getDataModified']; module.component(NAME, COMPONENT); diff --git a/@salix/crud/src/client/fiscal-data/index.js b/@salix/crud/src/client/fiscal-data/index.js index c589a4cba..1ea6c3515 100644 --- a/@salix/crud/src/client/fiscal-data/index.js +++ b/@salix/crud/src/client/fiscal-data/index.js @@ -8,13 +8,15 @@ export const COMPONENT = { bindings: { client: '<' }, - controller: function($http, copyObject, equalsObject, $transitions, $element) { + controller: function($http, copyObject, equalsObject, $transitions, $element, modified) { var self = this; var deregister = $transitions.onStart({ }, callback); this.submit = function() { if (!equalsObject(this.client, this.clientOld)) { - this.client.modify = "FiscalData"; + var newClient = modified(this.client, this.clientOld); + newClient.modify = "FiscalData"; + newClient.id = this.clientOld.id; $http.put('/client/api/Clients', this.client).then( json => { this.client = json.data; @@ -49,6 +51,6 @@ export const COMPONENT = { }; } }; -COMPONENT.controller.$inject = ['$http', 'copyObject', 'equalsObject', '$transitions', '$element']; +COMPONENT.controller.$inject = ['$http', 'copyObject', 'equalsObject', '$transitions', '$element', 'getDataModified']; module.component(NAME, COMPONENT); diff --git a/@salix/crud/src/client/web-access/index.js b/@salix/crud/src/client/web-access/index.js index 8c371ff76..987a91011 100644 --- a/@salix/crud/src/client/web-access/index.js +++ b/@salix/crud/src/client/web-access/index.js @@ -8,15 +8,16 @@ export const COMPONENT = { bindings: { client: '<' }, - controller: function($http, copyObject, equalsObject, $transitions, $element) { + controller: function($http, copyObject, equalsObject, $transitions, $element, modified) { var self = this; var deregister = $transitions.onStart({ }, callback); this.submit = function() { if (!equalsObject(this.account, this.accountOld)) { - this.client.modify = "WebAccess"; - this.account.id = this.client.id; - $http.put('/client/api/Accounts', this.account).then( + var newAccount = modified(this.account, this.accountOld); + newAccount.modify = "WebAccess"; + newAccount.id = this.client.id; + $http.put('/client/api/Accounts', newAccount).then( json => { this.account = json.data; self.copyAccount(); @@ -59,5 +60,5 @@ export const COMPONENT = { }; } }; -COMPONENT.controller.$inject = ['$http', 'copyObject', 'equalsObject', '$transitions', '$element']; +COMPONENT.controller.$inject = ['$http', 'copyObject', 'equalsObject', '$transitions', '$element', 'getDataModified']; module.component(NAME, COMPONENT);