From ab7b7dbf76d25401c4862171b3e54c9748b5098c Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 7 Apr 2010 14:14:25 -0700 Subject: ngswitch using --- src/widgets.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'src') 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; } }); -- cgit v1.2.3