Skip to content

Commit bc156da

Browse files
turned switch cases into methods
1 parent 4826b89 commit bc156da

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

react.js

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
var undefined;
1212

13-
window.react = {
13+
var react = {
1414

1515
nodes: {},
1616

@@ -113,41 +113,15 @@
113113
for(var whichDirective = 0; whichDirective < directives.length; whichDirective++){
114114
if(eachAncestor === node && (directiveIndex||0) <= whichDirective){ break; }
115115
if(!lastLink){ continue; }
116-
lastLink = this._extendScopeChainBasedOnDirective(lastLink, directives[whichDirective]);
116+
var directiveName = directives[whichDirective].shift();
117+
if(this._extendScopeChainBasedOnDirective[directiveName]){
118+
lastLink = this._extendScopeChainBasedOnDirective[directiveName](lastLink, directives[whichDirective]);
119+
}
117120
}
118121
}
119122
return lastLink;
120123
},
121124

122-
// given a scope chain and a directive, extends the scope chain if necessary
123-
// does not operate on anchor directives
124-
_extendScopeChainBasedOnDirective: function(lastLink, directive){
125-
// todo: turn these into named methods rather than a switch statement
126-
switch(directive[0]){
127-
case 'within':
128-
//todo: test that this isn't broken - it used to not do a lookup, only checked the last scope
129-
//todo: deprecate the suppressObservers flag
130-
return this._extendScopeChain(lastLink, this._lookupInScopeChain(directive[1], lastLink, {suppressObservers: true}), {type:'within', key: directive[1]});
131-
break;
132-
//todo: finish refactoring from here. asdf;
133-
case 'withinItem':
134-
// todo: write a test this for inadvertent fallthrough, as if it still said this._lookupInScopeChain(directive[1], lastLink, {suppressObservers: true})
135-
return this._extendScopeChain(lastLink, this.scopeChain.scope[directive[1]], {type:'withinItem', key: directive[1]}); //todo: changed from type:'within' - will that break anything?
136-
break;
137-
case 'bindItem':
138-
var itemBindings = {};
139-
if(directive.length === 4){
140-
itemBindings[directive[2]] = directive[1];
141-
}
142-
itemBindings[js.last(directive)] = new this._Fallthrough(directive[1]);
143-
return this._extendScopeChain(lastLink, itemBindings, {type:'itemBindings', key:directive[1]});
144-
break;
145-
default:
146-
return lastLink;
147-
break;
148-
}
149-
},
150-
151125
_buildScopeChainFromAnchorNames: function(names, lastLink){
152126
if(names){
153127
for(var whichToken = 1; whichToken < names.length; whichToken++){
@@ -485,6 +459,28 @@
485459
};
486460

487461

462+
// given a scope chain and a directive, extends the scope chain if necessary
463+
// does not operate on anchor directives
464+
react._extendScopeChainBasedOnDirective = js.create(react, {
465+
within: function(lastLink, args){
466+
//todo: test that this isn't broken - it used to not do a lookup, only checked the last scope
467+
//todo: deprecate the suppressObservers flag
468+
return this._extendScopeChain(lastLink, this._lookupInScopeChain(args[0], lastLink, {suppressObservers: true}), {type:'within', key: args[0]});
469+
},
470+
withinItem: function(lastLink, args){
471+
// todo: write a test this for inadvertent fallthrough, as if it still said this._lookupInScopeChain(args[0], lastLink, {suppressObservers: true})
472+
return this._extendScopeChain(lastLink, this.scopeChain.scope[args[0]], {type:'withinItem', key: args[0]}); //todo: changed from type:'within' - will that break anything?
473+
},
474+
bindItem: function(lastLink, args){
475+
var itemBindings = {};
476+
if(args.length === 3){
477+
itemBindings[args[1]] = args[0];
478+
}
479+
itemBindings[js.last(args)] = new this._Fallthrough(args[0]);
480+
return this._extendScopeChain(lastLink, itemBindings, {type:'itemBindings', key:args[0]});
481+
}
482+
}),
483+
488484
react.commands = js.create(react, {
489485

490486
/*
@@ -672,4 +668,6 @@
672668

673669
});
674670

671+
window.react = react;
672+
675673
}());

0 commit comments

Comments
 (0)