Skip to content

Commit

Permalink
Merge pull request #320 from spira/hotfix/entity-search-timeout
Browse files Browse the repository at this point in the history
Added timeout to entity search directive to allow digest cycle to clo…
  • Loading branch information
Jeremy Sik committed Dec 9, 2015
2 parents 6b5fe61 + 1fe611e commit 157867d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
7 changes: 5 additions & 2 deletions app/src/common/directives/entitySearch/entitySearch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace common.directives.entitySearch {

let $compile:ng.ICompileService,
$rootScope:ng.IRootScopeService,
$timeout:ng.ITimeoutService,
directiveScope:TestScope,
compiledElement:ng.IAugmentedJQuery,
directiveController:common.directives.entitySearch.EntitySearchController,
Expand All @@ -19,10 +20,11 @@ namespace common.directives.entitySearch {

module('app');

inject((_$compile_, _$rootScope_, _$q_) => {
inject((_$compile_, _$rootScope_, _$q_, _$timeout_) => {
$compile = _$compile_;
$rootScope = _$rootScope_;
$q = _$q_;
$timeout = _$timeout_;
});

// Only initialise the directive once to speed up the testing
Expand Down Expand Up @@ -92,6 +94,7 @@ namespace common.directives.entitySearch {
directiveController.selectedEntities[0] = newArticle;

(<any>directiveController).$scope.$apply();
(<any>directiveController).$timeout.flush();

expect(spyHandler).to.have.been.calledWith(newArticle);
spyHandler.restore();
Expand All @@ -104,14 +107,14 @@ namespace common.directives.entitySearch {
directiveController.selectedEntities = [];

(<any>directiveController).$scope.$apply();
(<any>directiveController).$timeout.flush();

expect(stubHandler).to.have.been.calledWith(null);
stubHandler.restore();

});



});

}
21 changes: 12 additions & 9 deletions app/src/common/directives/entitySearch/entitySearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace common.directives.entitySearch {

export class EntitySearchController {

static $inject = ['$mdDialog', '$scope', '$injector'];
static $inject = ['$mdDialog', '$scope', '$injector', '$timeout'];

public modelType:string;//bound by directive
public field:string;//bound by directive
Expand All @@ -31,11 +31,12 @@ namespace common.directives.entitySearch {
constructor(
private $mdDialog:ng.material.IDialogService,
private $scope:ng.IScope,
private $injector:ng.auto.IInjectorService
private $injector:ng.auto.IInjectorService,
private $timeout:ng.ITimeoutService
) {

this.entityService = this.$injector.get(this.modelType+'Service');

this.entitiesPaginator = this.entityService
.getPaginator()
.setCount(10);
Expand All @@ -46,12 +47,14 @@ namespace common.directives.entitySearch {

this.$scope.$watchCollection(() => this.selectedEntities, (newValue, oldValue) => {
if (!_.isEqual(newValue, oldValue)) {
if(!_.isEmpty(this.selectedEntities)) {
this.entityChangedHandler(this.selectedEntities[0]);
}
else {
this.entityChangedHandler(null);
}
this.$timeout(() => {
if(!_.isEmpty(this.selectedEntities)) {
this.entityChangedHandler(this.selectedEntities[0]);
}
else {
this.entityChangedHandler(null);
}
});
}
});
}
Expand Down

0 comments on commit 157867d

Please sign in to comment.