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

View File

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

View File

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

View File

@ -1,4 +1,5 @@
import EventEmitter from './event-emitter';
import {kebabToCamel} from './string';
/**
* Base class for component controllers.
@ -11,12 +12,26 @@ export default class Component extends EventEmitter {
* @param {$rootScope.Scope} $scope The element scope
*/
constructor($element, $scope) {
super($element, $scope);
super();
if (!$element) return;
this.element = $element[0];
this.element.$ctrl = this;
this.$element = $element;
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.
*/

View File

@ -1,21 +1,7 @@
import {kebabToCamel} from './string';
/**
* Implemented by all those classes that emit events.
*/
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.
*

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 './modified';
import './key-codes';
import './get-watchers';
import './http-error';
import './user-error';