forked from marcusphillips/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact.js
710 lines (620 loc) · 26.3 KB
/
react.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*!
* React for JavaScript - an easy-rerender template language
* Version 1.0.1, http://github.com/marcusphillips/react
*
* Copyright 2010, Marcus Phillips
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function () {
var undefined;
var react = {
nodes: {},
scopes: {},
_matchers: {
directiveDelimiter: /\s*,\s*/,
space: /\s+/,
isString: /(^'.*'$)|(^".*"$)/,
negation: /!\s*/,
isNumber: /\d+/
},
name: function(name, object){
this.scopes[name] = object;
},
getNodeKey: function(node){
return (node.reactKey = node.reactKey || js.util.unique('reactNode'));
},
getScopeKey: function(object){
return (object.reactKey = object.reactKey || js.util.unique('reactObject'));
},
getObjectKey: function(){
throw new Error('This method is deprecated - please use getScopeKey() instead');
},
set: function(object, key, value){
object[key] = value;
this.changed(object, key);
},
changed: function(object, key){
// if no key us supplied, check every key
if(arguments.length < 2){
for(key in object){
this.changed(object, key);
}
// todo: this still won't work for arguments lists
if(Object.prototype.toString.call(object) === '[object Array]'){
this.changed(object, 'length');
}
return;
}
// if there are no observers for the supplied key, do nothing
if(!object || !object.observers || !object.observers[key]){ return; }
for(var listenerString in object.observers[key]){
this._checkListener(object, key, listenerString);
}
},
_checkListener: function(object, key, listenerString){
var listener = this._interpretListenerString(listenerString);
if(!this._listenerIsStillValid(listener, object, key)){ return; }
// todo: bindItem is needed here but won't work until the registration is made on the array element it's bound to. something like
js.errorIf(listener.directive[0] === 'bindItem', 'you need recalculations for bindItem (when the key was an itemAlias), but those aren\'t implemented yet');
if(js.among(['within', 'withinEach', 'withinItem', 'for'], listener.directive[0])){
// todo: loopKey probably won't work, and maybe withinEach either
this._updateTree({
node: listener.node,
fromDirective: listener.directiveIndex
});
return;
}
var nodesToUpdate = [];
var updateContext = js.create(this.commands, {
root: listener.node, // todo: is this right? root seems meaningless in this case. only added root to the updateNode method so I could allow updating of whole branch at once
node: listener.node,
nodesToUpdate: nodesToUpdate,
scopeChain: listener.scopeChain,
// todo: this probably needs a pushscope method
// todo: consolidate all these updateContext object creations
// todo: these last two probably don't belong here. they were added to keep .enqueueNodes() from erroring.
bequeathedScopeChains: {},
loopItemTemplates: {}
});
var directiveContext = js.create(updateContext, {
directiveIndex: listener.directiveIndex,
});
this._followDirective(listener.directive, directiveContext);
this._updateNodes(nodesToUpdate, updateContext);
},
_interpretListenerString: function(listenerString){
var listener = listenerString.split(' ');
var node = this.nodes[listener[0]];
var directiveIndex = +listener[1];
return{
node: node,
directiveIndex: directiveIndex,
prefix: listener[2],
directive: this._getDirectives(node)[directiveIndex],
scopeChain: this._buildScopeChainForNode(node, directiveIndex)
};
},
_listenerIsStillValid: function(listener, object, key){
// ignore the object if it's not in the same path that lead to registration of a listener
return object === this._lookupInScopeChain(listener.prefix+key, listener.scopeChain, {returnObject: true});
},
_buildScopeChainForNode: function(node, directiveIndex){
var ancestors = $(Array.prototype.reverse.apply($(node).parents())).add(node);
var scopeBuildingContext = js.create(react.commands, {
//todo: deprecate the suppressObservers flag
suppressObservers: true
});
for(var whichAncestor = 0; whichAncestor < ancestors.length; whichAncestor++){
scopeBuildingContext.node = ancestors[whichAncestor];
var directives = this._getDirectives(scopeBuildingContext.node);
scopeBuildingContext.scopeChain = this._buildScopeChainFromAnchorNames(directives.anchored, scopeBuildingContext.scopeChain);
var pushScope = function(scope, options){
scopeBuildingContext.scopeChain = this._extendScopeChain(scopeBuildingContext.scopeChain, scope, options);
};
for(var whichDirective = 0; whichDirective < directives.length; whichDirective++){
if(scopeBuildingContext.node === node && (directiveIndex||0) <= whichDirective){ break; }
if(!scopeBuildingContext.scopeChain){ continue; }
if(js.among(['within', 'withinEach', 'bindItem'], directives[whichDirective][0])){
var directiveContext = js.create(scopeBuildingContext, {
directiveIndex: whichDirective,
pushScope: pushScope
});
this._followDirective(directives[whichDirective], directiveContext);
}
}
}
return scopeBuildingContext.scopeChain;
},
_buildScopeChainFromAnchorNames: function(names, lastLink){
if(names){
for(var whichToken = 1; whichToken < names.length; whichToken++){
var scopeKey = names[whichToken];
js.errorIf(!this.scopes[scopeKey], 'could not follow anchored directive, nothing found at react.scopes.'+scopeKey);
lastLink = this._extendScopeChain(lastLink, this.scopes[scopeKey], {type:'anchor', key: scopeKey});
}
}
return lastLink;
},
_buildScopeChain: function(scopes, options){
options = options || {};
var lastLink = options.prefix;
if(scopes){
for(var which = 0; which < scopes.length; which++){
lastLink = this._extendScopeChain(lastLink, scopes[which], options);
}
}
return lastLink;
},
_extendScopeChain: function(link, additionalScope, options){
options = options || {};
return {
parent: link,
scope: additionalScope,
type: options.type,
key: options.key,
anchorKey: options.type === 'anchor' ? options.key : (link||{}).anchorKey
};
},
update: function(/*[node, scope],*/ options){
options = options || {};
if(options.nodeType){
// detect argument signature of (node, scope)
options = {
node: arguments[0],
scope: arguments[1]
};
js.merge(options, arguments[2] || {});
}
return this._updateTree(options);
},
_updateTree: function(options){
var root = options.node;
//todo: test these
//js.errorIf(!root, 'no root supplied to update()');
//js.errorIf(this.isNode(root), 'first argument supplied to react.update() must be a dom node');
js.errorIf(options.scope && options.scopes, 'you must supply only one set of scopes');
var updateContext = js.create(this.commands, {
root: root,
nodesToUpdate: Array.prototype.slice.apply(root.querySelectorAll('[react]')),
bequeathedScopeChains: {},
loopItemTemplates: {}
});
var scopes = options.scope ? [options.scope] : options.scopes ? options.scopes : undefined;
if(options.anchor){
this.anchor({node: root, scopes:scopes});
scopes = undefined;
}
var baseScopeChain = this._buildScopeChain(scopes, {type: 'updateInputs', prefix: this._buildScopeChainForNode(root, options.fromDirective || 0)});
updateContext.bequeathedScopeChains[this.getNodeKey(root)] = this._updateNodeGivenScopeChain(root, baseScopeChain, updateContext, options.fromDirective);
this._updateNodes(updateContext.nodesToUpdate, updateContext);
return root;
},
_updateNodes: function(nodes, updateContext){
for(var i = 0; i < nodes.length; i++){
this._updateNode(nodes[i], updateContext);
}
},
_enqueueNodes: function(newNodes){
this.nodesToUpdate.push.apply(this.nodesToUpdate, newNodes);
for(var whichNode = 0; whichNode < newNodes.length; whichNode++){
var nodeKey = this.getNodeKey(newNodes[whichNode]);
delete this.bequeathedScopeChains[nodeKey];
delete this.loopItemTemplates[nodeKey];
}
},
_getParent: function(node, updateContext){
var ancestor = $(node).parent()[0];
var repeatLimit = 1000;
while(repeatLimit--){
if(!ancestor || ancestor === document){
return false;
} else if (
ancestor === updateContext.root ||
ancestor.getAttribute('react') ||
updateContext.bequeathedScopeChains[this.getNodeKey(ancestor)] || // todo: what's this cover?
updateContext.loopItemTemplates[this.getNodeKey(ancestor)] // todo: I don't think we need this now that it gets a special class attached to it
){
return ancestor;
}
ancestor = $(ancestor).parent()[0];
}
js.error('_getParent() broke');
},
_updateNode: function(node, updateContext){
//todo: test that you never revisit a node
var nodeKey = this.getNodeKey(node);
if(
typeof updateContext.bequeathedScopeChains[nodeKey] !== 'undefined' ||
node === updateContext.root // this is to prevent an undefined scope chain for the root getting overwritten with false. don't like it.
){
// node has already been visited
return;
}
if(updateContext.loopItemTemplates[this.getNodeKey(node)]){ // todo: get rid of all these references to 'loop item templates', use custom class instead
updateContext.bequeathedScopeChains[nodeKey] = false;
return;
}
var previousParent = 'unmatchable';
var parent = this._getParent(node, updateContext);
// if processing the parent leads to this node having a new parent, repeat
while(parent !== previousParent){
if(!parent){
updateContext.bequeathedScopeChains[nodeKey] = false;
return;
}
this._updateNode(parent, updateContext);
if(updateContext.bequeathedScopeChains[this.getNodeKey(parent)] === false){
updateContext.bequeathedScopeChains[nodeKey] = false;
return;
}
previousParent = parent;
parent = this._getParent(node, updateContext);
}
var scopeChain = updateContext.bequeathedScopeChains[this.getNodeKey(parent)];
updateContext.bequeathedScopeChains[nodeKey] = this._updateNodeGivenScopeChain(node, scopeChain, updateContext);
},
_updateNodeGivenScopeChain: function(node, scopeChain, updateContext, fromDirective){
var nodeKey = this.getNodeKey(node);
var directives = this._getDirectives(node);
if(directives.anchored){
scopeChain = this._buildScopeChainFromAnchorNames(directives.anchored, scopeChain);
}
var pushScope = function(scope, options){
scopeChain = this._extendScopeChain(scopeChain, scope, options);
};
for(var i = fromDirective || 0; i < directives.length; i++){
var directiveContext = js.create(updateContext, {
node: node,
directiveIndex: i,
scopeChain: scopeChain,
pushScope: pushScope
});
this._followDirective(directives[i], directiveContext);
}
return scopeChain;
},
_getDirectives: function(node){
var directiveStrings = (node.getAttribute('react')||'').split(this._matchers.directiveDelimiter);
var that = this;
var directives = js.map(directiveStrings, function(which, string){
return js.trim(string).replace(that._matchers.negation, '!').split(that._matchers.space);
});
if(directives[0] && directives[0][0] === 'anchored'){
var anchored = directives.shift();
}
directives = js.filter(directives, function(directive){
return !!directive[0];
});
directives.anchored = anchored;
return directives;
},
_setDirectives: function(node, directives){
var anchored = directives.anchored;
directives = js.filter(directives, function(directive){
return !!directive[0];
});
directives.anchored = anchored;
if(directives.anchored){
directives.unshift(directives.anchored);
}
var directiveStrings = js.map(directives, function(which, directive){
return directive.join(' ');
});
node.setAttribute('react', directiveStrings.join(', '));
},
_prependDirective: function(node, directive){
var directives = this._getDirectives(node);
directives.unshift(directive);
this._setDirectives(node, directives);
},
_followDirective: function(directive, context){
try{
var command = directive.shift();
js.errorIf(!this.commands[command], command+' is not a valid react command');
this.commands[command].apply(context, directive);
}catch (error){
js.errorIf(typeof context.directiveIndex !== 'number', 'You tried to follow a directive without supplying a directive index in the execution context');
var directive = this._getDirectives(context.node)[context.directiveIndex];
js.log('Failure during React update: ', {
'original error': error,
'while processing node': context.node,
'index of failed directive': context.directiveIndex,
'directive call': directive[0]+'('+directive.slice(1).join(', ')+')',
'scope chain description': this._describeScopeChain(context.scopeChain),
'(internal scope chain object) ': context.scopeChain
});
throw error;
}
},
_describeScopeChain: function(link){
var scopeChainDescription = [];
while(link){
scopeChainDescription.push(['scope: ', link.scope, ', type of scope shift: ' + link.type + (link.key ? ' (key: '+link.key+')': '') + (link.anchorKey ? ', anchored to: '+link.anchorKey+')': '')]);
link = link.parent;
}
return scopeChainDescription;
},
anchor: function(options){
options = options || {};
if(options.nodeType){
options = {
node: arguments[0],
scope: arguments[1]
};
}
var node = options.node;
var scopes = options.scope ? [options.scope] : options.scopes;
var nodeKey = this.getNodeKey(node);
this.nodes[nodeKey] = node;
var directives = this._getDirectives(node);
// todo: clean up after any existing anchor
directives.anchored = ['anchored'];
for(var i = 0; i < scopes.length; i++){
var scopeKey = this.getScopeKey(scopes[i]);
this.scopes[scopeKey] = scopes[i];
directives.anchored.push(scopeKey);
}
this._setDirectives(node, directives);
return options.node;
},
_observeScope: function(object, prefix, key, node, directiveIndex, anchorKey, didMatch){
// todo: scope observers per node-object anchoring, for easy cleanup of memory references
var nodeKey = this.getNodeKey(node);
this.nodes[nodeKey] = node;
var observations = node['directive ' + directiveIndex + ' observes'] = node['directive ' + directiveIndex + ' observes'] || [];
observations.push({object: object, key: key, didMatch: didMatch});
object.observers = object.observers || {};
object.observers[key] = object.observers[key] || {};
object.observers[key][nodeKey + ' ' + directiveIndex + ' ' + prefix] = true;
},
_disregardScope: function(node, directiveIndex){
// todo: check this, it might be jank
var nodeKey = this.getNodeKey(node);
var observations = node['directive ' + directiveIndex + ' observes'];
for(var whichObservation = 0; whichObservation < observations.length; whichObservation++){
var observation = observations[whichObservation];
delete observation.object.observers[observation.key][nodeKey + ' ' + directiveIndex];
}
delete nodes.observing[directiveIndex];
if(!js.size(nodes.observing)){
delete this.nodes[nodeKey];
}
},
_Fallthrough: function(key){
this.key = key;
},
_lookupInScopeChain: function(key, scopeChain, options){
if(!scopeChain){
return;
}
options = options || {};
var negate;
var value;
if(key[0] === '!'){
negate = true;
key = key.slice(1);
}
if (this._matchers.isString.test(key)) {
return key.slice(1, key.length-1);
}
// todo: clean up any pre-existing observers
var keys = key.split('.');
var baseKey = keys.shift();
// the search paths list holds a set of namespaces
do {
var object = scopeChain.scope;
value = object[baseKey];
// todo: write a test to verify that responses to change events don't result in new observers
if(scopeChain.anchorKey && options.listener && !this.suppressObservers){
this._observeScope(object, '', baseKey, options.listener.node, options.listener.directiveIndex, scopeChain.anchorKey, value !== undefined);
}
if(value instanceof this._Fallthrough){
baseKey = value.key;
}else if(value !== undefined){
break;
}
}while((scopeChain = scopeChain.parent));
var prefix = baseKey + '.';
// one for each segment of the dot acess
while(keys.length){
object = value;
if(object === undefined || object === null){
return options.returnObject ? false : js.error('can\'t find keys '+keys.join('.')+' on an undefined object');
}
if(scopeChain.anchorKey && !options.returnObject && !this.suppressObservers){
this._observeScope(object, prefix, keys[0], options.listener.node, options.listener.directiveIndex, scopeChain.anchorKey, true);
}
prefix = prefix + keys[0] + '.';
value = object[keys.shift()];
}
if(options.returnObject){
return object;
}
if(typeof value === 'function'){ value = value.call(object); }
return negate ? ! value : value;
}
};
react.integrate = {
jQuery: function(){
jQuery.fn.update = function(scope){
react.update(this, scope);
};
}
};
react.commands = js.create(react, {
/*
* when a command runs, it will have a 'this' scope like the following (arrows indicate prototype relationships
*
* react {
* }
*
* ^
* |
* commands {
* command handler definitions
* lookup(key)
* }
*
* ^
* |
* // a new processing scope is created for each node to be updated
* nodeContext {
* node
* scopeChain
* }
*/
lookup: function(key, options){
options = options || {};
options.listener = {
node: this.node,
directiveIndex: this.directiveIndex
};
return this._lookupInScopeChain(key, this.scopeChain, options);
},
anchored: function(token){
this.pushScope(this.scopes[token], {type:'anchor', key:token});
},
within: function(key){
// todo: port and test this
// js.errorIf(typeof scope !== 'object' && typeof scope !== 'array' && typeof scope !== 'function', 'mask commands must receive a namespacing value');
this.pushScope(this.lookup(key), {type:'within', key:key});
},
contain: function(key){
// using innerHTML to clear the node because the jQuery convenience functions unbind event handlers. This would be an unexpected side effect for most React user consumption cases.
this.node.innerHTML = '';
var insertion = this.lookup(key);
// if the insertion is a node, use the dom appending method, but insert other items as text
if(insertion && insertion.nodeType){
jQuery(this.node).append(insertion);
this._enqueueNodes(this._getReactNodes(insertion));
} else {
jQuery(this.node).text(insertion);
}
},
_getReactNodes: function(root){
return [root].concat(Array.prototype.slice.apply(root.querySelectorAll('[react]')));
},
classIf: function(conditionKey, nameKey){
this.node.classIfs = this.node.classIfs || {};
var condition = this.lookup(conditionKey);
var className;
var persistence = conditionKey + ' ' + nameKey;
if(condition){
className = this.lookup(nameKey);
if(className){
$(this.node).addClass(className);
this.node.classIfs[persistence] = className;
}
} else {
className = this.node.classIfs[persistence] || this.lookup(nameKey);
if(className){
$(this.node).removeClass(className);
delete this.node.classIfs[persistence];
}
}
},
_createItemNodes: function(makeDirective){
var $loopChildren = jQuery(this.node).children();
js.errorIf($loopChildren.length < 2, 'looping nodes must contain at least 2 children - one item template and one results container');
var $itemTemplate = $loopChildren.first();
//js.errorIf(this._getDirectives($itemTemplate[0])[0].join(' ') !== 'itemTemplate', 'the item template must declare itself with an item directive');
$itemTemplate.addClass('reactItemTemplate');
this.loopItemTemplates[this.getNodeKey($itemTemplate[0])] = $itemTemplate[0];
var $resultsContainer = $($loopChildren[1]);
var $resultsContents = $resultsContainer.children();
// todo: ignore binding scopes when looking for scope to iterate over
var collection = this.scopeChain.scope;
// todo: don't allow looping over static native objects (like strings - this is almost certainly an error)
js.errorIf(collection === null || collection === undefined, 'The loop command expected a collection, but instead encountered '+collection);
if(this.scopeChain.anchorKey && !this.suppressObservers){
// todo: optimize. this is a shortcut - it simply puts a listener on the length property that results in a complete re-render of the looping directive if ever a change in length is noticed
this._observeScope(collection, '', 'length', this.node, this.directiveIndex, this.scopeChain.anchorKey, true);
}
var itemNodes = [];
for(var i = 0; i < collection.length; i++){
var itemNode = $resultsContents[i];
if(!itemNode){
itemNode = $itemTemplate.clone().removeClass('reactItemTemplate')[0];
// todo: implement bindings as key aliases
js.errorIf(this._matchers.space.test(i), 'looping not currently supported over colletions with space-filled keys'); // todo: make this even more restrictive - just alphanumerics
var itemDirective = makeDirective(i);
this._prependDirective(itemNode, itemDirective);
this._enqueueNodes(this._getReactNodes(itemNode));
}
itemNodes.push(itemNode);
}
if(collection.length !== $resultsContents.length){
// we set innerHTML here to prevent jQuery fron detaching all event handlers (automatic in an .html() call)
$resultsContainer[0].innerHTML = '';
$resultsContainer.html(itemNodes);
}
},
withinEach: function(){
// todo: return here (and everywhere else) if collection is undefined. test for this
this._createItemNodes(function(index){
return ['withinItem', index];
});
},
withinItem: function(key){
// todo: add a rule to only allow getting items from last scope (check if key < scope.length?)
// todo: add a rule to make sure the last scope object is an array
js.errorIf(this.scopeChain.scope.length-1 < +key, 'Tried to re-render a node for an index the no longer exists');
// todo: want to raise an error including link to this.scopeChain.scope - write an error helper
js.errorIf(!this.scopeChain.scope[key], 'Could not find anything at key '+key+' on the scope object');
// todo: might be a problem that using the within() as a helper will give the scope a type of 'within'
this.within(key);
},
'for': function(keyAlias, valueAlias){
var aliases = arguments;
// todo: return here (and everywhere else) if collection is undefined. test for this
this._createItemNodes(function(index){
return ['bindItem', index].concat(Array.prototype.slice.call(aliases));
});
},
bindItem: function(key, keyAlias, valueAlias){
if(valueAlias === undefined){
valueAlias = keyAlias;
keyAlias = undefined;
}
// set up an item scope to be applied for each item
// a new scope will be created with bindings for valueAlias and optionally for keyAlias
var itemBindings = {};
if(keyAlias !== undefined){
itemBindings[keyAlias] = key;
}
// todo: don't make this a fallthrough - create an explicit binding to the previous array scope object
itemBindings[valueAlias] = new this._Fallthrough(key);
this.pushScope(itemBindings, {type:'bindItem', key:key});
},
showIf: function(condition){
jQuery(this.node)[this.lookup(condition) ? 'show' : 'hide']();
},
visIf: function(condition){
jQuery(this.node).css('visibility', this.lookup(condition) ? 'visible' : 'hidden');
},
attr: function(name, value){
js.errorIf(arguments.length !== 2, 'the attr directive requires 2 arguments');
name = this.lookup(name);
value = this.lookup(value);
if(!js.among(['string', 'number'], typeof name)){
js.log('bad attr name: ', name);
js.error('expected attr name token ' + name + ' to resolve to a string or number, not ' + typeof name);
}else if(!js.among(['string', 'number'], typeof value)){
js.log('bad attr value: ', value);
js.error('expected attr value token ' + value + ' to resolve to a string or number not, not ' + typeof value);
}
jQuery(this.node).attr(name, value);
},
attrIf: function(condition, name, value){
if(this.lookup(condition)){
$(this.node).attr(this.lookup(name), this.lookup(value));
} else {
$(this.node).removeAttr(this.lookup(name));
}
},
checkedIf: function(condition){
$(this.node).attr('checked', this.lookup(condition));
}
});
window.react = react;
}());