Skip to content
This repository has been archived by the owner on Jan 22, 2023. It is now read-only.

Commit

Permalink
chore: use $applyAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasduteil committed Jan 8, 2015
1 parent 491369a commit 035e40c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/ui-ace.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,14 @@ angular.module('ui.ace', [])
onChange: function (callback) {
return function (e) {
var newValue = session.getValue();
if (newValue !== scope.$eval(attrs.value)) {
if (angular.isDefined(ngModel)) {

if (ngModel && newValue !== ngModel.$viewValue) {
scope.$applyAsync(function () {
ngModel.$setViewValue(newValue);
!scope.$$phase && !scope.$root.$$phase && scope.$digest();
}
executeUserCallback(callback, e, acee);
});
}

executeUserCallback(callback, e, acee);
};
},
/**
Expand All @@ -248,7 +249,7 @@ angular.module('ui.ace', [])
});

// Value Blind
if (ngModel !== null) {
if (ngModel) {
ngModel.$formatters.push(function (value) {
if (angular.isUndefined(value) || value === null) {
return '';
Expand Down
28 changes: 28 additions & 0 deletions test/ace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,36 @@ describe('uiAce', function () {

var value = 'baz';
_ace.getSession().setValue(value);
scope.$apply();

expect(scope.foo).toBe(value);
});

it('should update the IDE only if different', function () {
scope.change = jasmine.createSpy('scope.change');

$compile('<div ui-ace ng-model="foo" ng-change="change(foo)">')(scope);

// change shouldn't be called initialy
expect(scope.change).not.toHaveBeenCalled();

// change shouldn't be called when the value change is coming from the model.
scope.$apply('foo = "bar"');
expect(scope.change).not.toHaveBeenCalled();

_ace.getSession().setValue('baz');
scope.$apply();

// ace removeText event + ace insertText event
expect(scope.change.calls.count()).toBe(2);
// ace removeText event
expect(scope.change).toHaveBeenCalledWith('');
// ace insertText event
expect(scope.change).toHaveBeenCalledWith('baz');

//
expect(scope.foo).toBe('baz');
});
});

describe('when the model is undefined/null', function () {
Expand Down

0 comments on commit 035e40c

Please sign in to comment.