refactor watcher

This commit is contained in:
dherrero 2017-10-05 09:20:40 +02:00
parent dd004fdc89
commit 601cf6db25
5 changed files with 72 additions and 22 deletions

View File

@ -22,7 +22,7 @@
"state": "clientCard.basicData",
"component": "vn-client-basic-data",
"params": {
"client": "card.client"
"client": "$ctrl.client"
},
"menu": {
"description": "Datos básicos",
@ -33,7 +33,7 @@
"state": "clientCard.fiscalData",
"component": "vn-client-fiscal-data",
"params": {
"client": "card.client"
"client": "$ctrl.client"
},
"menu": {
"description": "Datos fiscales",
@ -44,7 +44,7 @@
"state": "clientCard.billingData",
"component": "vn-client-billing-data",
"params": {
"client": "card.client"
"client": "$ctrl.client"
},
"menu": {
"description": "Datos facturación",
@ -60,7 +60,7 @@
"state": "clientCard.addresses.list",
"component": "vn-client-addresses",
"params": {
"client": "card.client"
"client": "$ctrl.client"
},
"menu": {
"description": "Consignatarios",
@ -79,7 +79,7 @@
"state": "clientCard.webAccess",
"component": "vn-client-web-access",
"params": {
"client": "card.client"
"client": "$ctrl.client"
},
"menu": {
"description": "Acceso web",
@ -95,7 +95,7 @@
"state": "clientCard.notes.list",
"component": "vn-client-notes",
"params": {
"client": "card.client"
"client": "$ctrl.client"
},
"menu": {
"description": "Notas",

View File

@ -1,13 +1,13 @@
<vn-horizontal>
<mg-ajax
path="/client/api/Clients/{{edit.params.id}}/card"
actions="card.client = edit.model"
actions="$ctrl.client = edit.model"
options="mgEdit">
</mg-ajax>
<vn-empty style="min-width: 18em; padding-left: 1em; padding-bottom: 1em;">
<vn-descriptor
client="card.client"
active="card.client.active"
client="$ctrl.client"
active="$ctrl.client.active"
class="display-block" >
</vn-descriptor>
<vn-left-menu></vn-left-menu>

View File

@ -9,6 +9,5 @@ export default class Controller {
ngModule.component('vnClientCard', {
template: require('./card.html'),
controller: Controller,
controllerAs: 'card'
controller: Controller
});

View File

@ -1,6 +1,48 @@
import {module} from '../module';
const isEqual = angular.equals;
export const myEqual = (objA, objB, estrict) => {
let equals = true;
let estrictMode = estrict || true;
if (Object.keys(objA).length !== Object.keys(objB).length) {
return false;
}
for (let k in objA) {
if (!objB.hasOwnProperty(k)) {
return false;
}
if (estrictMode) {
if (typeof objA[k] !== typeof objB[k]) {
return false;
}
if (typeof objA[k] !== 'object' && objA[k] !== objB[k]) {
return false;
}
if (typeof objA[k] === 'object') {
equals = myEqual(objA[k], objB[k], estrictMode);
if (!equals) {
return false;
}
}
} else {
if (typeof objA[k] !== 'object' && objA[k] != objB[k]) {
return false;
}
if (typeof objA[k] === 'object') {
equals = myEqual(objA[k], objB[k], estrictMode);
if (!equals) {
return false;
}
}
}
}
return equals;
};
export default isEqual;
export const NAME = 'equalsObject';

View File

@ -1,8 +1,8 @@
import {module} from '../module';
import Component from '../lib/component';
import getModifiedData from '../lib/modified';
// import copyObject from '../lib/copy';
import isEqual from '../lib/equals';
import copyObject from '../lib/copy';
import {myEqual} from '../lib/equals';
/**
* Component that checks for changes on a specific model property and
@ -11,12 +11,13 @@ import isEqual from '../lib/equals';
* properties are provided.
*/
export default class Watcher extends Component {
constructor($element, $scope, $state, $transitions, $http, vnApp, $translate) {
constructor($element, $scope, $state, $transitions, $http, vnApp, $translate, $timeout) {
super($element);
this.$scope = $scope;
this.$state = $state;
this.$http = $http;
this.$translate = $translate;
this.$timeout = $timeout;
this.vnApp = vnApp;
this.state = null;
@ -48,7 +49,7 @@ export default class Watcher extends Component {
// return new Promise((resolve, reject) => {
this.$http.get(`${this.url}/${id}`).then(
json => {
this.data = this.copyObject(json.data);
this.data = copyObject(json.data);
this.copyData();
}
// json => reject(json)
@ -126,11 +127,13 @@ export default class Watcher extends Component {
}
writeData(json, resolve) {
Object.keys(this.data).forEach(
/* Object.keys(this.data).forEach(
key => {
this.data[key] = json.data[key];
}
);
);*/
this.data = Object.assign(this.data, json.data);
this.copyData();
resolve(json);
@ -159,8 +162,13 @@ export default class Watcher extends Component {
if (data) {
Object.keys(data).forEach(
val => {
if (data[val] !== "" && data[val] !== undefined && data[val] !== null)
copy[val] = data[val];
if (data[val] !== "" && data[val] !== undefined && data[val] !== null) {
if (typeof data[val] === 'object') {
copy[val] = this.copyObject(data[val]);
} else {
copy[val] = data[val];
}
}
}
);
}
@ -180,20 +188,21 @@ export default class Watcher extends Component {
dataChanged() {
let newData = this.copyObject(this.data);
return !isEqual(newData, this.orgData);
return !myEqual(newData, this.orgData);
}
onConfirmResponse(response) {
if (response === 'ACCEPT') {
this.data = this.copyObject(this.orgData);
Object.assign(this.data, this.orgData);
this.$state.go(this.state);
} else {
this.state = null;
}
}
}
Watcher.$inject = ['$element', '$scope', '$state', '$transitions', '$http', 'vnApp', '$translate'];
Watcher.$inject = ['$element', '$scope', '$state', '$transitions', '$http', 'vnApp', '$translate', '$timeout'];
module.component('vnWatcher', {
template: require('./watcher.html'),