Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit ab7b7db

Browse files
author
Misko Hevery
committed
ngswitch using
1 parent 29309e0 commit ab7b7db

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

angular-debug.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ function toJsonArray(buf, obj, pretty, stack){
492492
}
493493
}
494494
/**
495-
= * Template provides directions an how to bind to a given element.
495+
* Template provides directions an how to bind to a given element.
496496
* It contains a list of init functions which need to be called to
497497
* bind to a new instance of elements. It also provides a list
498498
* of child paths which contain child templates
@@ -1902,6 +1902,7 @@ JQLite.prototype = {
19021902
});
19031903
},
19041904

1905+
//TODO: remove
19051906
trigger: function(type) {
19061907
var evnt = document.createEvent('MouseEvent');
19071908
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){
32813282
}
32823283
});
32833284

3284-
angularWidget('NG:SWITCH', function(element){
3285+
angularWidget('NG:SWITCH', function ngSwitch(element){
32853286
var compiler = this,
32863287
watchExpr = element.attr("on"),
3288+
whenFn = ngSwitch[element.attr("using") || 'equals'];
3289+
changeExpr = element.attr('change') || '',
32873290
cases = [];
3291+
if (!whenFn) throw "Using expression '" + usingExpr + "' unknown.";
32883292
eachNode(element, function(caseElement){
32893293
var when = caseElement.attr('ng-switch-when');
32903294
if (when) {
32913295
cases.push({
3292-
when: function(value){ return value == when; },
3296+
when: function(scope, value){
3297+
return whenFn.call(scope, value, when);
3298+
},
3299+
change: changeExpr,
32933300
element: caseElement,
32943301
template: compiler.compile(caseElement)
32953302
});
@@ -3301,10 +3308,13 @@ angularWidget('NG:SWITCH', function(element){
33013308
this.$watch(watchExpr, function(value){
33023309
element.html('');
33033310
childScope = null;
3311+
var params = {};
33043312
foreach(cases, function(switchCase){
3305-
if (switchCase.when(value)) {
3313+
if (switchCase.when(params, value)) {
33063314
element.append(switchCase.element);
33073315
childScope = createScope(scope);
3316+
extend(childScope, params);
3317+
childScope.$tryEval(switchCase.change, element);
33083318
switchCase.template(switchCase.element, childScope);
33093319
childScope.$init();
33103320
}
@@ -3314,6 +3324,30 @@ angularWidget('NG:SWITCH', function(element){
33143324
if (childScope) childScope.$eval();
33153325
});
33163326
};
3327+
}, {
3328+
equals: function(on, when) {
3329+
return on == when;
3330+
},
3331+
route: function(on, when) {
3332+
var regex = '^' + when.replace(/[\.\\\(\)\^\$]/g, "\$1") + '$', params = [], self = this;
3333+
foreach(when.split(/\W/), function(param){
3334+
if (param) {
3335+
var paramRegExp = new RegExp(":" + param + "([\\W])");
3336+
if (regex.match(paramRegExp)) {
3337+
regex = regex.replace(paramRegExp, "(.*)$1");
3338+
params.push(param);
3339+
}
3340+
}
3341+
});
3342+
console.log(regex);
3343+
var match = on.match(new RegExp(regex));
3344+
if (match) {
3345+
foreach(params, function(name, index){
3346+
self[name] = match[index + 1];
3347+
});
3348+
}
3349+
return match;
3350+
}
33173351
});
33183352
angularService("$window", bind(window, identity, window));
33193353
angularService("$document", function(window){

src/widgets.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,12 @@ angularWidget('NG:SWITCH', function ngSwitch(element){
206206
this.$watch(watchExpr, function(value){
207207
element.html('');
208208
childScope = null;
209+
var params = {};
209210
foreach(cases, function(switchCase){
210-
if (switchCase.when(childScope, value)) {
211+
if (switchCase.when(params, value)) {
211212
element.append(switchCase.element);
212213
childScope = createScope(scope);
214+
extend(childScope, params);
213215
childScope.$tryEval(switchCase.change, element);
214216
switchCase.template(switchCase.element, childScope);
215217
childScope.$init();
@@ -225,7 +227,23 @@ angularWidget('NG:SWITCH', function ngSwitch(element){
225227
return on == when;
226228
},
227229
route: function(on, when) {
228-
this.name = 'misko';
229-
return true;
230+
var regex = '^' + when.replace(/[\.\\\(\)\^\$]/g, "\$1") + '$', params = [], self = this;
231+
foreach(when.split(/\W/), function(param){
232+
if (param) {
233+
var paramRegExp = new RegExp(":" + param + "([\\W])");
234+
if (regex.match(paramRegExp)) {
235+
regex = regex.replace(paramRegExp, "(.*)$1");
236+
params.push(param);
237+
}
238+
}
239+
});
240+
console.log(regex);
241+
var match = on.match(new RegExp(regex));
242+
if (match) {
243+
foreach(params, function(name, index){
244+
self[name] = match[index + 1];
245+
});
246+
}
247+
return match;
230248
}
231249
});

0 commit comments

Comments
 (0)