Skip to content
This repository was archived by the owner on May 25, 2019. It is now read-only.

Commit 14f6954

Browse files
whitehat101douglasduteil
authored andcommitted
feat(directive): add instance access throught $broadcast event
Merge pull request #53 from whitehat101/instance-access-through-broadcast new feature: allow access to the CM instance through a $broadcast event Close #53
1 parent 84cfe5a commit 14f6954

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/ui-codemirror.js

+12
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ angular.module('ui.codemirror', [])
110110
});
111111
}
112112

113+
114+
// Allow access to the CodeMirror instance through a broadcasted event
115+
// eg: $broadcast('CodeMirror', function(cm){...});
116+
scope.$on('CodeMirror', function(event, callback){
117+
if (angular.isFunction(callback)) {
118+
callback(codeMirror);
119+
} else {
120+
throw new Error('the CodeMirror event requires a callback function');
121+
}
122+
});
123+
124+
113125
// onLoad callback
114126
if (angular.isFunction(opts.onLoad)) {
115127
opts.onLoad(codeMirror);

test/codemirror.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,17 @@ describe('uiCodemirror', function () {
247247
expect(scope.codemirrorLoaded).toHaveBeenCalledWith(codemirror);
248248
});
249249

250+
it('responds to the $broadcast event "CodeMirror"', function () {
251+
var broadcast = { callback: angular.noop };
252+
spyOn(broadcast, 'callback');
253+
254+
$compile('<div ui-codemirror></div>')(scope);
255+
scope.$broadcast('CodeMirror', broadcast.callback);
256+
257+
expect(broadcast.callback).toHaveBeenCalled();
258+
expect(broadcast.callback).toHaveBeenCalledWith(codemirror);
259+
});
260+
250261

251262
it('should watch the options', function () {
252263
spyOn(scope, '$watch').andCallThrough();

0 commit comments

Comments
 (0)