Skip to content

Commit

Permalink
fix(#1309): Scope.bindToContext() will be applied on scope if no cont…
Browse files Browse the repository at this point in the history
…ext is specified, and add specs
  • Loading branch information
DAB0mB committed Apr 8, 2016
1 parent e316e00 commit 210f16a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Thumbs.db
.build*
.npm
node_modules
npm-debug.log

# IDE's #
#########
Expand Down
5 changes: 5 additions & 0 deletions src/modules/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ angular.module(name, [

// Binds an object or a function to the provided context and digest it once it is invoked
$$Core.$bindToContext = function(context, fn) {
if (_.isFunction(context)) {
fn = context;
context = this;
}

return $$utils.bind(fn, context, this.$$throttledDigest.bind(this));
};

Expand Down
37 changes: 37 additions & 0 deletions tests/integration/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,5 +340,42 @@ describe('angular-meteor.core', function() {
scope.applyMethod('method', ['foo', 'bar'], angular.noop);
});
});

describe('$bindToContext()', function() {
var scope;

beforeEach(function() {
scope = $rootScope.$new();
});

afterEach(function() {
scope.$destroy();
});

it('should bind a function to scope and digest once invoked', function() {
var fn = jasmine.createSpy('function');
scope.$digest = jasmine.createSpy('digest');

var boundFn = scope.$bindToContext(fn);
boundFn(1, 2, 3);

expect(fn).toHaveBeenCalled();
expect(fn.calls.mostRecent().object).toEqual(scope);
expect(fn.calls.mostRecent().args).toEqual([1, 2, 3]);
});

it('should bind the function to a custom scope if specified', function() {
var fn = jasmine.createSpy('function');
scope.$digest = jasmine.createSpy('digest');

var context = {};
var boundFn = scope.$bindToContext(context, fn);
boundFn(1, 2, 3);

expect(fn).toHaveBeenCalled();
expect(fn.calls.mostRecent().object).toEqual(context);
expect(fn.calls.mostRecent().args).toEqual([1, 2, 3]);
});
});
});
});

0 comments on commit 210f16a

Please sign in to comment.