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

View File

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

View File

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

View File

@ -1,6 +1,48 @@
import {module} from '../module'; import {module} from '../module';
const isEqual = angular.equals; 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 default isEqual;
export const NAME = 'equalsObject'; export const NAME = 'equalsObject';

View File

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