-
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 '$ionicBind' service, which takes an object containing binding definitions (similar to angular directive isolate scope definition). Allows binding of any directive attribute & expressions from a scope, letting us do normal attribute -> scope binding without having to create isolate scopes. Closes #555. Closes #669
- Loading branch information
Showing
7 changed files
with
268 additions
and
35 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('$ionicBind', ['$parse', '$interpolate', function($parse, $interpolate) { | ||
var LOCAL_REGEXP = /^\s*([@=&])(\??)\s*(\w*)\s*$/; | ||
return function(scope, attrs, bindDefinition) { | ||
angular.forEach(bindDefinition || {}, 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])(scope); | ||
} | ||
break; | ||
|
||
case '=': | ||
if (!attrs[attrName]) { | ||
return; | ||
} | ||
unwatch = scope.$watch(attrs[attrName], function(value) { | ||
scope[scopeName] = value; | ||
}); | ||
//Destroy parent scope watcher when this scope is destroyed | ||
scope.$on('$destroy', unwatch); | ||
break; | ||
|
||
case '&': | ||
/* jshint -W044 */ | ||
if (attrs[attrName] && attrs[attrName].match(RegExp(scopeName + '\(.*?\)'))) { | ||
throw new Error('& expression binding "' + scopeName + '" looks like it will recursively call "' + | ||
attrs[attrName] + '" and cause a stack overflow! Please choose a different scopeName.'); | ||
} | ||
parentGet = $parse(attrs[attrName]); | ||
scope[scopeName] = function(locals) { | ||
return parentGet(scope, 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
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
Oops, something went wrong.