aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMisko Hevery2010-04-07 14:14:25 -0700
committerMisko Hevery2010-04-07 14:14:25 -0700
commitab7b7dbf76d25401c4862171b3e54c9748b5098c (patch)
tree3cf6892b65f29c57e24f75db892533ce1fff9e5b
parent29309e0e5a5f5eafd0f948100417d63127d3332d (diff)
downloadangular.js-ab7b7dbf76d25401c4862171b3e54c9748b5098c.tar.bz2
ngswitch using
-rw-r--r--angular-debug.js42
-rw-r--r--src/widgets.js24
2 files changed, 59 insertions, 7 deletions
diff --git a/angular-debug.js b/angular-debug.js
index f3353eae..e23352dd 100644
--- a/angular-debug.js
+++ b/angular-debug.js
@@ -492,7 +492,7 @@ function toJsonArray(buf, obj, pretty, stack){
}
}
/**
-= * Template provides directions an how to bind to a given element.
+ * Template provides directions an how to bind to a given element.
* It contains a list of init functions which need to be called to
* bind to a new instance of elements. It also provides a list
* of child paths which contain child templates
@@ -1902,6 +1902,7 @@ JQLite.prototype = {
});
},
+ //TODO: remove
trigger: function(type) {
var evnt = document.createEvent('MouseEvent');
evnt.initMouseEvent(type, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
@@ -3281,15 +3282,21 @@ angularWidget('NG:INCLUDE', function(element){
}
});
-angularWidget('NG:SWITCH', function(element){
+angularWidget('NG:SWITCH', function ngSwitch(element){
var compiler = this,
watchExpr = element.attr("on"),
+ whenFn = ngSwitch[element.attr("using") || 'equals'];
+ changeExpr = element.attr('change') || '',
cases = [];
+ if (!whenFn) throw "Using expression '" + usingExpr + "' unknown.";
eachNode(element, function(caseElement){
var when = caseElement.attr('ng-switch-when');
if (when) {
cases.push({
- when: function(value){ return value == when; },
+ when: function(scope, value){
+ return whenFn.call(scope, value, when);
+ },
+ change: changeExpr,
element: caseElement,
template: compiler.compile(caseElement)
});
@@ -3301,10 +3308,13 @@ angularWidget('NG:SWITCH', function(element){
this.$watch(watchExpr, function(value){
element.html('');
childScope = null;
+ var params = {};
foreach(cases, function(switchCase){
- if (switchCase.when(value)) {
+ if (switchCase.when(params, value)) {
element.append(switchCase.element);
childScope = createScope(scope);
+ extend(childScope, params);
+ childScope.$tryEval(switchCase.change, element);
switchCase.template(switchCase.element, childScope);
childScope.$init();
}
@@ -3314,6 +3324,30 @@ angularWidget('NG:SWITCH', function(element){
if (childScope) childScope.$eval();
});
};
+}, {
+ equals: function(on, when) {
+ return on == when;
+ },
+ route: function(on, when) {
+ var regex = '^' + when.replace(/[\.\\\(\)\^\$]/g, "\$1") + '$', params = [], self = this;
+ foreach(when.split(/\W/), function(param){
+ if (param) {
+ var paramRegExp = new RegExp(":" + param + "([\\W])");
+ if (regex.match(paramRegExp)) {
+ regex = regex.replace(paramRegExp, "(.*)$1");
+ params.push(param);
+ }
+ }
+ });
+ console.log(regex);
+ var match = on.match(new RegExp(regex));
+ if (match) {
+ foreach(params, function(name, index){
+ self[name] = match[index + 1];
+ });
+ }
+ return match;
+ }
});
angularService("$window", bind(window, identity, window));
angularService("$document", function(window){
diff --git a/src/widgets.js b/src/widgets.js
index 012f13e2..0f781f2c 100644
--- a/src/widgets.js
+++ b/src/widgets.js
@@ -206,10 +206,12 @@ angularWidget('NG:SWITCH', function ngSwitch(element){
this.$watch(watchExpr, function(value){
element.html('');
childScope = null;
+ var params = {};
foreach(cases, function(switchCase){
- if (switchCase.when(childScope, value)) {
+ if (switchCase.when(params, value)) {
element.append(switchCase.element);
childScope = createScope(scope);
+ extend(childScope, params);
childScope.$tryEval(switchCase.change, element);
switchCase.template(switchCase.element, childScope);
childScope.$init();
@@ -225,7 +227,23 @@ angularWidget('NG:SWITCH', function ngSwitch(element){
return on == when;
},
route: function(on, when) {
- this.name = 'misko';
- return true;
+ var regex = '^' + when.replace(/[\.\\\(\)\^\$]/g, "\$1") + '$', params = [], self = this;
+ foreach(when.split(/\W/), function(param){
+ if (param) {
+ var paramRegExp = new RegExp(":" + param + "([\\W])");
+ if (regex.match(paramRegExp)) {
+ regex = regex.replace(paramRegExp, "(.*)$1");
+ params.push(param);
+ }
+ }
+ });
+ console.log(regex);
+ var match = on.match(new RegExp(regex));
+ if (match) {
+ foreach(params, function(name, index){
+ self[name] = match[index + 1];
+ });
+ }
+ return match;
}
});