-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ionContent): use child scope instead of isolate scope
Adds new '$ionicBindParent' service, which takes an object containing binding definitions (similar to angular directive isolate scope definition). Allows binding of any parent scope & directive attributes to a child scope, letting us do normal parent -> child scope binding without having to create isolate scopes. Closes #555
- Loading branch information
Showing
6 changed files
with
260 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
angular.module('ionic.service.bind', []) | ||
.factory('$ionicBindFromParent', ['$parse', '$interpolate', function($parse, $interpolate) { | ||
var LOCAL_REGEXP = /^\s*([@=&])(\??)\s*(\w*)\s*$/; | ||
return function(scope, attrs, bindings) { | ||
var parentScope = scope.$parent; | ||
if (!parentScope) { | ||
throw new Error('Cannot bind to $rootScope!'); | ||
} | ||
|
||
angular.forEach(bindings || {}, function (definition, scopeName) { | ||
//Adapted from angular.js $compile | ||
var match = definition.match(LOCAL_REGEXP) || [], | ||
attrName = match[3] || scopeName, | ||
mode = match[1], // @, =, or & | ||
parentGet, | ||
unwatch; | ||
|
||
switch(mode) { | ||
case '@': | ||
if (!attrs[attrName]) { | ||
return; | ||
} | ||
attrs.$observe(attrName, function(value) { | ||
scope[scopeName] = value; | ||
}); | ||
// we trigger an interpolation to ensure | ||
// the value is there for use immediately | ||
if (attrs[attrName]) { | ||
scope[scopeName] = $interpolate(attrs[attrName])(parentScope); | ||
} | ||
break; | ||
|
||
case '=': | ||
if (!attrs[attrName]) { | ||
return; | ||
} | ||
unwatch = parentScope.$watch(attrs[attrName], function(value) { | ||
scope[scopeName] = value; | ||
}); | ||
//Destroy parent scope watcher when this scope is destroyed | ||
scope.$on('$destroy', unwatch); | ||
break; | ||
|
||
case '&': | ||
parentGet = $parse(attrs[attrName]); | ||
scope[scopeName] = function(locals) { | ||
return parentGet(parentScope, locals); | ||
}; | ||
break; | ||
} | ||
}); | ||
}; | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
js/ext/angular/test/service/ionicBindFromParent.unit.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
describe('$ionicBindFromParent', function() { | ||
beforeEach(module('ionic.service.bind')); | ||
|
||
var $bind, scope, attr, $observeFn; | ||
beforeEach(inject(function($ionicBindFromParent, $rootScope, $interpolate) { | ||
$bind = $ionicBindFromParent; | ||
scope = $rootScope.$new(); | ||
attr = { | ||
$observe: jasmine.createSpy('observe').andCallFake(function(name, fn) { | ||
$observeFn = fn; | ||
}) | ||
}; | ||
})); | ||
|
||
it('should error if rootScope', inject(function($rootScope) { | ||
expect(function() { | ||
$bind($rootScope, {}, {}); | ||
}).toThrow(); | ||
})); | ||
|
||
describe('= bind', function() { | ||
|
||
it('should bind expression to scope', function() { | ||
scope.$parent.coffee = 2; | ||
attr.eq = 'coffee'; | ||
$bind(scope, attr, { | ||
eq: '=' | ||
}); | ||
scope.$apply(); | ||
expect(scope.eq).toEqual(2); | ||
scope.$parent.$apply('coffee = 100'); | ||
expect(scope.eq).toEqual(100); | ||
}); | ||
|
||
it('should allow binding a different name to scope', function() { | ||
scope.$parent.coffee = 2; | ||
attr.eq = 'coffee'; | ||
$bind(scope, attr, { | ||
coolVar: '=eq' | ||
}); | ||
scope.$apply(); | ||
expect(scope.coolVar).toEqual(scope.$parent.coffee); | ||
scope.$parent.$apply('coffee = 100'); | ||
expect(scope.coolVar).toEqual(100); | ||
}); | ||
|
||
it('should work as expected if bind name is same', function() { | ||
scope.$parent.foo = 2; | ||
attr.espresso = 'foo'; | ||
$bind(scope, attr, { | ||
espresso: '=' | ||
}); | ||
scope.$apply(); | ||
expect(scope.foo).toBe(2); | ||
scope.$parent.$apply('foo = 4'); | ||
expect(scope.foo).toBe(4); | ||
}); | ||
|
||
it('should unwatch on $destroy', function() { | ||
var watchUnregister = jasmine.createSpy('watchUnreg'); | ||
spyOn(scope.$parent, '$watch').andCallFake(function() { | ||
return watchUnregister; | ||
}); | ||
attr.binding = 'something'; | ||
$bind(scope, attr, { | ||
binding: '=' | ||
}); | ||
scope.$destroy(); | ||
expect(watchUnregister).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe ('@ bind', function() { | ||
|
||
it('should bind expression to scope', function() { | ||
scope.$parent.coffee = 'cool'; | ||
attr.special = '{{coffee}}'; | ||
$bind(scope, attr, { | ||
special: '@' | ||
}); | ||
expect(attr.$observe).toHaveBeenCalledWith('special', $observeFn); | ||
expect(scope.special).toBe('cool'); | ||
scope.$parent.coffee = 'espresso'; | ||
$observeFn(scope.$parent.coffee); | ||
expect(scope.special).toBe('espresso'); | ||
}); | ||
|
||
it('should allow binding a different name to scope', function() { | ||
scope.$parent.coffee = 'cool'; | ||
attr.special = '{{coffee}}'; | ||
$bind(scope, attr, { | ||
scopeName: '@special' | ||
}); | ||
expect(scope.scopeName).toBe('cool'); | ||
scope.$parent.coffee = 'espresso'; | ||
$observeFn(scope.$parent.coffee); | ||
expect(scope.scopeName).toBe('espresso'); | ||
}); | ||
|
||
it('should allow binding a different name to scope', function() { | ||
scope.$parent.coffee = 'cool'; | ||
attr.special = '{{coffee}}'; | ||
$bind(scope, attr, { | ||
coffee: '@special' | ||
}); | ||
expect(scope.coffee).toBe('cool'); | ||
scope.$parent.coffee = 'espresso'; | ||
$observeFn(scope.$parent.coffee); | ||
expect(scope.coffee).toBe('espresso'); | ||
}); | ||
|
||
}); | ||
|
||
describe('& bind', function() { | ||
|
||
it('should bind expression to scope', function() { | ||
attr.math = '1+1'; | ||
$bind(scope, attr, { | ||
two: '&math' | ||
}); | ||
expect(scope.two()).toBe(2); | ||
}); | ||
|
||
it('should bind expression with different name to scope', function() { | ||
attr.doIt = 'fun()'; | ||
scope.$parent.fun = function() { | ||
return 'this is cool!'; | ||
}; | ||
$bind(scope, attr, { | ||
party: '&doIt' | ||
}); | ||
expect(scope.party()).toBe('this is cool!'); | ||
}); | ||
|
||
it('should work as expected if scopeNames are the same', function() { | ||
scope.$parent.fn = function() { return 1; }; | ||
attr.bad = 'fn()'; | ||
$bind(scope, attr, { | ||
fn: '&bad' | ||
}); | ||
expect(scope.fn()).toBe(1); | ||
}); | ||
}); | ||
}); |