This repository was archived by the owner on Sep 8, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +36
-8
lines changed
Expand file tree Collapse file tree 2 files changed +36
-8
lines changed Original file line number Diff line number Diff line change @@ -218,14 +218,14 @@ angular.module('ui.ace', [])
218218 onChange : function ( callback ) {
219219 return function ( e ) {
220220 var newValue = session . getValue ( ) ;
221- if ( newValue !== scope . $eval ( attrs . value ) && ! scope . $$phase && ! scope . $root . $$phase ) {
222- if ( ngModel !== null ) {
223- scope . $apply ( function ( ) {
224- ngModel . $setViewValue ( newValue ) ;
225- } ) ;
226- }
227- executeUserCallback ( callback , e , acee ) ;
221+
222+ if ( ngModel && newValue !== ngModel . $viewValue ) {
223+ scope . $applyAsync ( function ( ) {
224+ ngModel . $setViewValue ( newValue ) ;
225+ } ) ;
228226 }
227+
228+ executeUserCallback ( callback , e , acee ) ;
229229 } ;
230230 } ,
231231 /**
@@ -249,7 +249,7 @@ angular.module('ui.ace', [])
249249 } ) ;
250250
251251 // Value Blind
252- if ( ngModel !== null ) {
252+ if ( ngModel ) {
253253 ngModel . $formatters . push ( function ( value ) {
254254 if ( angular . isUndefined ( value ) || value === null ) {
255255 return '' ;
Original file line number Diff line number Diff line change @@ -216,8 +216,36 @@ describe('uiAce', function () {
216216
217217 var value = 'baz' ;
218218 _ace . getSession ( ) . setValue ( value ) ;
219+ scope . $apply ( ) ;
220+
219221 expect ( scope . foo ) . toBe ( value ) ;
220222 } ) ;
223+
224+ it ( 'should update the IDE only if different' , function ( ) {
225+ scope . change = jasmine . createSpy ( 'scope.change' ) ;
226+
227+ $compile ( '<div ui-ace ng-model="foo" ng-change="change(foo)">' ) ( scope ) ;
228+
229+ // change shouldn't be called initialy
230+ expect ( scope . change ) . not . toHaveBeenCalled ( ) ;
231+
232+ // change shouldn't be called when the value change is coming from the model.
233+ scope . $apply ( 'foo = "bar"' ) ;
234+ expect ( scope . change ) . not . toHaveBeenCalled ( ) ;
235+
236+ _ace . getSession ( ) . setValue ( 'baz' ) ;
237+ scope . $apply ( ) ;
238+
239+ // ace removeText event + ace insertText event
240+ expect ( scope . change . calls . count ( ) ) . toBe ( 2 ) ;
241+ // ace removeText event
242+ expect ( scope . change ) . toHaveBeenCalledWith ( '' ) ;
243+ // ace insertText event
244+ expect ( scope . change ) . toHaveBeenCalledWith ( 'baz' ) ;
245+
246+ //
247+ expect ( scope . foo ) . toBe ( 'baz' ) ;
248+ } ) ;
221249 } ) ;
222250
223251 describe ( 'when the model is undefined/null' , function ( ) {
You can’t perform that action at this time.
0 commit comments