EventEmitter fixes

This commit is contained in:
Juan 2018-10-22 17:54:04 +02:00
parent 4cf74664d5
commit 4d97f4b43e
7 changed files with 27 additions and 53 deletions

View File

@ -2,7 +2,7 @@
vn-id="model" vn-id="model"
url="claim/api/ClaimEnds" url="claim/api/ClaimEnds"
filter="$ctrl.filter" filter="$ctrl.filter"
data="$ctrl.salesClaimed" on-data-change="$ctrl.onDataChange()"> data="$ctrl.salesClaimed">
</vn-crud-model> </vn-crud-model>
<vn-vertical> <vn-vertical>
<vn-card pad-large> <vn-card pad-large>
@ -22,7 +22,6 @@
ng-click="$ctrl.importToNewRefundTicket()" ng-click="$ctrl.importToNewRefundTicket()"
vn-tooltip="Imports claim details"> vn-tooltip="Imports claim details">
</vn-button> </vn-button>
<vn-button <vn-button
label="Import ticket" label="Import ticket"
ng-click="$ctrl.showLastTickets($event)" ng-click="$ctrl.showLastTickets($event)"

View File

@ -87,13 +87,11 @@ describe('claim', () => {
describe('setClaimDestination(id, claimDestinationFk)', () => { describe('setClaimDestination(id, claimDestinationFk)', () => {
it('should make a patch and call refresh and showSuccess', () => { it('should make a patch and call refresh and showSuccess', () => {
spyOn(controller.$.model, 'refresh');
spyOn(controller.vnApp, 'showSuccess'); spyOn(controller.vnApp, 'showSuccess');
$httpBackend.expectPATCH(`claim/api/ClaimEnds/`).respond({}); $httpBackend.expectPATCH(`claim/api/ClaimEnds/`).respond({});
controller.setClaimDestination(1, 1); controller.setClaimDestination(1, 1);
$httpBackend.flush(); $httpBackend.flush();
expect(controller.$.model.refresh).toHaveBeenCalledWith();
expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!'); expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
}); });
}); });

View File

@ -1,5 +1,5 @@
import ngModule from '../../module'; import ngModule from '../../module';
import EventEmitter from '../../lib/event-emitter'; import Component from '../../lib/component';
/** /**
* A filtrable model. * A filtrable model.
@ -35,7 +35,7 @@ export class Paginable {
* *
* @event dataChange Emitted when data property changes * @event dataChange Emitted when data property changes
*/ */
export class DataModel extends EventEmitter { export class DataModel extends Component {
get data() {} get data() {}
refresh() {} refresh() {}
clear() {} clear() {}
@ -46,7 +46,6 @@ ngModule.component('vnDataModel', {
bindings: { bindings: {
data: '=?', data: '=?',
autoLoad: '<?', autoLoad: '<?',
autoSync: '<?',
order: '@?', order: '@?',
limit: '<?' limit: '<?'
} }
@ -119,7 +118,13 @@ export default class ModelProxy extends DataModel {
} }
createRow(obj, index, orgRow) { createRow(obj, index, orgRow) {
let proxy = new Proxy(obj, { Object.assign(obj, {
$orgIndex: index,
$orgRow: orgRow,
$oldData: null,
$isNew: false
});
return new Proxy(obj, {
set: (obj, prop, value) => { set: (obj, prop, value) => {
if (prop.charAt(0) !== '$' && value !== obj[prop] && !obj.$isNew) { if (prop.charAt(0) !== '$' && value !== obj[prop] && !obj.$isNew) {
if (!obj.$oldData) if (!obj.$oldData)
@ -127,17 +132,11 @@ export default class ModelProxy extends DataModel {
if (!obj.$oldData[prop]) if (!obj.$oldData[prop])
obj.$oldData[prop] = value; obj.$oldData[prop] = value;
this.isChanged = true; this.isChanged = true;
this.emit('rowChange', {obj, prop, value});
} }
return Reflect.set(obj, prop, value); return Reflect.set(obj, prop, value);
} }
}); });
Object.assign(proxy, {
$orgIndex: index,
$orgRow: orgRow,
$oldData: null,
$isNew: false
});
return proxy;
} }
resetChanges() { resetChanges() {

View File

@ -1,4 +1,5 @@
import EventEmitter from './event-emitter'; import EventEmitter from './event-emitter';
import {kebabToCamel} from './string';
/** /**
* Base class for component controllers. * Base class for component controllers.
@ -11,12 +12,26 @@ export default class Component extends EventEmitter {
* @param {$rootScope.Scope} $scope The element scope * @param {$rootScope.Scope} $scope The element scope
*/ */
constructor($element, $scope) { constructor($element, $scope) {
super($element, $scope); super();
if (!$element) return;
this.element = $element[0]; this.element = $element[0];
this.element.$ctrl = this; this.element.$ctrl = this;
this.$element = $element; this.$element = $element;
this.$ = $scope; this.$ = $scope;
} }
$postLink() {
if (!this.$element) return;
let attrs = this.$element[0].attributes;
let $scope = this.$;
for (let attr of attrs) {
if (attr.name.substr(0, 2) !== 'on') continue;
let eventName = kebabToCamel(attr.name.substr(3));
let callback = locals => $scope.$parent.$eval(attr.nodeValue, locals);
this.on(eventName, callback);
}
}
/** /**
* The component owner window. * The component owner window.
*/ */

View File

@ -1,21 +1,7 @@
import {kebabToCamel} from './string';
/** /**
* Implemented by all those classes that emit events. * Implemented by all those classes that emit events.
*/ */
export default class EventEmitter { export default class EventEmitter {
$postLink() {
if (!this.$element) return;
let attrs = this.$element[0].attributes;
let $scope = this.$;
for (let attr of attrs) {
if (attr.name.substr(0, 2) !== 'on') continue;
let eventName = kebabToCamel(attr.name.substr(3));
let callback = locals => $scope.$parent.$eval(attr.nodeValue, locals);
this.on(eventName, callback);
}
}
/** /**
* Connects to an object event. * Connects to an object event.
* *

View File

@ -1,22 +0,0 @@
function getWatchers(root) {
root = angular.element(root || document.documentElement);
function getElemWatchers(element) {
let isolateWatchers = getWatchersFromScope(element.data().$isolateScope);
let scopeWatchers = getWatchersFromScope(element.data().$scope);
let watchers = scopeWatchers.concat(isolateWatchers);
angular.forEach(element.children(), childElement => {
watchers = watchers.concat(getElemWatchers(angular.element(childElement)));
});
return watchers;
}
function getWatchersFromScope(scope) {
if (scope)
return scope.$$watchers || [];
return [];
}
return getElemWatchers(root).length;
}
export default getWatchers;

View File

@ -11,6 +11,5 @@ import './copy';
import './equals'; import './equals';
import './modified'; import './modified';
import './key-codes'; import './key-codes';
import './get-watchers';
import './http-error'; import './http-error';
import './user-error'; import './user-error';