Skip to content

Commit 210f16a

Browse files
committed
fix(#1309): Scope.bindToContext() will be applied on scope if no context is specified, and add specs
1 parent e316e00 commit 210f16a

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Thumbs.db
1010
.build*
1111
.npm
1212
node_modules
13+
npm-debug.log
1314

1415
# IDE's #
1516
#########

src/modules/core.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ angular.module(name, [
125125

126126
// Binds an object or a function to the provided context and digest it once it is invoked
127127
$$Core.$bindToContext = function(context, fn) {
128+
if (_.isFunction(context)) {
129+
fn = context;
130+
context = this;
131+
}
132+
128133
return $$utils.bind(fn, context, this.$$throttledDigest.bind(this));
129134
};
130135

tests/integration/core.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,5 +340,42 @@ describe('angular-meteor.core', function() {
340340
scope.applyMethod('method', ['foo', 'bar'], angular.noop);
341341
});
342342
});
343+
344+
describe('$bindToContext()', function() {
345+
var scope;
346+
347+
beforeEach(function() {
348+
scope = $rootScope.$new();
349+
});
350+
351+
afterEach(function() {
352+
scope.$destroy();
353+
});
354+
355+
it('should bind a function to scope and digest once invoked', function() {
356+
var fn = jasmine.createSpy('function');
357+
scope.$digest = jasmine.createSpy('digest');
358+
359+
var boundFn = scope.$bindToContext(fn);
360+
boundFn(1, 2, 3);
361+
362+
expect(fn).toHaveBeenCalled();
363+
expect(fn.calls.mostRecent().object).toEqual(scope);
364+
expect(fn.calls.mostRecent().args).toEqual([1, 2, 3]);
365+
});
366+
367+
it('should bind the function to a custom scope if specified', function() {
368+
var fn = jasmine.createSpy('function');
369+
scope.$digest = jasmine.createSpy('digest');
370+
371+
var context = {};
372+
var boundFn = scope.$bindToContext(context, fn);
373+
boundFn(1, 2, 3);
374+
375+
expect(fn).toHaveBeenCalled();
376+
expect(fn.calls.mostRecent().object).toEqual(context);
377+
expect(fn.calls.mostRecent().args).toEqual([1, 2, 3]);
378+
});
379+
});
343380
});
344381
});

0 commit comments

Comments
 (0)