Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit c99a159

Browse files
committed
Merge branch 'marshall007-global-load'
2 parents de4d1c6 + 74afaef commit c99a159

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

src/ui-ace.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ angular.module('ui.ace', [])
7979
]);
8080
}
8181

82-
// onLoad callback
83-
if (angular.isFunction(opts.onLoad)) {
84-
opts.onLoad(acee);
85-
}
82+
// onLoad callbacks
83+
angular.forEach(opts.callbacks, function (cb) {
84+
if (angular.isFunction(cb)) {
85+
cb(acee);
86+
}
87+
});
8688

8789
// Basic options
8890
if (angular.isString(opts.theme)) {
@@ -272,15 +274,19 @@ angular.module('ui.ace', [])
272274
};
273275
}
274276

275-
// set the options here, even if we try to watch later, if this
276-
// line is missing things go wrong (and the tests will also fail)
277-
setOptions(acee, session, opts);
278-
279277
// Listen for option updates
280-
scope.$watch( attrs.uiAce, function(current, previous) {
278+
var updateOptions = function (current, previous) {
281279
if (current === previous) return;
282280
opts = angular.extend({}, options, scope.$eval(attrs.uiAce));
283281

282+
opts.callbacks = [ opts.onLoad ];
283+
if (opts.onLoad !== options.onLoad) {
284+
// also call the global onLoad handler
285+
opts.callbacks.unshift(options.onLoad);
286+
}
287+
288+
// EVENTS
289+
284290
// unbind old change listener
285291
session.removeListener('change', onChangeListener);
286292

@@ -297,14 +303,13 @@ angular.module('ui.ace', [])
297303
acee.on('blur', onBlurListener);
298304

299305
setOptions(acee, session, opts);
300-
}, /* deep watch */ true );
306+
};
301307

302-
// EVENTS
303-
onChangeListener = listenerFactory.onChange(opts.onChange);
304-
session.on('change', onChangeListener);
308+
scope.$watch(attrs.uiAce, updateOptions, /* deep watch */ true);
305309

306-
onBlurListener = listenerFactory.onBlur(opts.onBlur);
307-
acee.on('blur', onBlurListener);
310+
// set the options here, even if we try to watch later, if this
311+
// line is missing things go wrong (and the tests will also fail)
312+
updateOptions(options);
308313

309314
elm.on('$destroy', function () {
310315
acee.session.$stopWorker();

test/ace.spec.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,37 @@ describe('uiAce', function () {
167167
});
168168
});
169169
describe('onLoad', function () {
170-
it('runs the onLoad callback', function () {
171-
scope.aceLoaded = function () {
172-
};
173-
spyOn(scope, 'aceLoaded');
170+
it('should call the local onLoad callback', function () {
171+
scope.aceLoaded = jasmine.createSpy('scope.aceLoaded');
174172
$compile('<div ui-ace="{onLoad: aceLoaded}">')(scope);
175173
expect(scope.aceLoaded).toHaveBeenCalled();
176174
expect(scope.aceLoaded).toHaveBeenCalledWith(_ace);
177175
});
176+
177+
it('should call the global onLoad callback', function () {
178+
uiConfig.ace.onLoad = jasmine.createSpy('uiConfig.ace.onLoad');
179+
$compile('<div ui-ace>')(scope);
180+
expect(uiConfig.ace.onLoad).toHaveBeenCalled();
181+
expect(uiConfig.ace.onLoad).toHaveBeenCalledWith(_ace);
182+
});
183+
184+
it('should call both local/global onLoad callbacks', function () {
185+
scope.aceLoaded = jasmine.createSpy('scope.aceLoaded');
186+
uiConfig.ace.onLoad = jasmine.createSpy('uiConfig.ace.onLoad');
187+
$compile('<div ui-ace="{onLoad: aceLoaded}">')(scope);
188+
expect(uiConfig.ace.onLoad).toHaveBeenCalled();
189+
expect(uiConfig.ace.onLoad).toHaveBeenCalledWith(_ace);
190+
expect(scope.aceLoaded).toHaveBeenCalled();
191+
expect(scope.aceLoaded).toHaveBeenCalledWith(_ace);
192+
});
193+
194+
it('should detect same local/global onLoad callback', function () {
195+
uiConfig.ace.onLoad = jasmine.createSpy('uiConfig.ace.onLoad');
196+
scope.aceLoaded = uiConfig.ace.onLoad;
197+
$compile('<div ui-ace="{onLoad: aceLoaded}">')(scope);
198+
expect(uiConfig.ace.onLoad.calls.count()).toBe(1);
199+
expect(uiConfig.ace.onLoad).toHaveBeenCalledWith(_ace);
200+
});
178201
});
179202
});
180203

0 commit comments

Comments
 (0)