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

Commit 946f52a

Browse files
committed
Merge pull request #78 from angular-ui/fix-refresh-codemirror-in-the-next-event-loop
fix: refresh codemirror in next event loop
2 parents c2cc9d6 + 1c03cac commit 946f52a

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/ui-codemirror.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ angular.module('ui.codemirror', [])
77
.constant('uiCodemirrorConfig', {})
88
.directive('uiCodemirror', uiCodemirrorDirective);
99

10-
function uiCodemirrorDirective(uiCodemirrorConfig) {
10+
function uiCodemirrorDirective($timeout, uiCodemirrorConfig) {
1111

1212
return {
1313
restrict: 'EA',
@@ -138,7 +138,7 @@ function uiCodemirrorDirective(uiCodemirrorConfig) {
138138
scope.$watch(uiRefreshAttr, function(newVal, oldVal) {
139139
// Skip the initial watch firing
140140
if (newVal !== oldVal) {
141-
codeMirror.refresh();
141+
$timeout(codeMirror.refresh);
142142
}
143143
});
144144
}

test/codemirror.spec.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ describe('uiCodemirror', function() {
22
'use strict';
33

44
// declare these up here to be global to all tests
5-
var scope, $compile, uiConfig;
5+
var scope, $compile, $timeout, uiConfig;
66
var codemirrorDefaults = window.CodeMirror.defaults;
77

88
beforeEach(function() {
99
module('ui.codemirror');
1010

11-
inject(function(_$rootScope_, _$compile_, uiCodemirrorConfig) {
11+
inject(function(_$rootScope_, _$compile_, _$timeout_, uiCodemirrorConfig) {
1212
scope = _$rootScope_.$new();
1313
$compile = _$compile_;
14+
$timeout = _$timeout_;
1415
uiConfig = uiCodemirrorConfig;
1516
});
1617

@@ -237,15 +238,19 @@ describe('uiCodemirror', function() {
237238

238239
scope.$apply('bar = false');
239240
expect(scope.bar).toBeFalsy();
241+
$timeout.flush();
240242
expect(codemirror.refresh).toHaveBeenCalled();
241243
scope.$apply('bar = true');
242244
expect(scope.bar).toBeTruthy();
245+
$timeout.flush();
243246
expect(codemirror.refresh).toHaveBeenCalled();
244247
scope.$apply('bar = 0');
245248
expect(scope.bar).toBeFalsy();
249+
$timeout.flush();
246250
expect(codemirror.refresh).toHaveBeenCalled();
247251
scope.$apply('bar = 1');
248252
expect(scope.bar).toBeTruthy();
253+
$timeout.flush();
249254
expect(codemirror.refresh).toHaveBeenCalled();
250255

251256
expect(codemirror.refresh.calls.count()).toEqual(4);

0 commit comments

Comments
 (0)