/
tbone.js
1759 lines (1628 loc) · 59.5 KB
/
tbone.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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function () {
/** @const {boolean} */
var TBONE_DEBUG = window['TBONE_DEBUG'];
var tbone = function (arg0, arg1, arg2) {
if (arg0) {
if (typeof arg0 === 'function') {
return autorun(arg0, arg1, arg2);
} else {
return lookup.apply(this, arguments);
}
}
/**
* Does anything make sense to do with no arguments?
*/
};
var data = {};
var models = {};
var collections = {};
var templates = {};
var views = {};
/**
* Scheduling priority constants
*
* The scheduler will update views and models in this order:
* 1) synchronous (local) models
* 2) views
* 3) asynchronous (ajax) models
*
* The goals of this ordering are:
* - never render a view based on an outdated model that
* we can update immediately.
* - defer ajax requests until we know that something in the
* UI needs its data.
*/
/** @const */
var BASE_PRIORITY_MODEL_SYNC = 3000;
/** @const */
var BASE_PRIORITY_VIEW = 2000;
/** @const */
var BASE_PRIORITY_MODEL_ASYNC = 1000;
/**
* We also use the processQueue to initialize models & views. By adding this delta
* to priorities for initialization, we ensure that initialization happens in the
* same order as execution and that it happens before execution. For example, it
* may be inefficient for a model to reset before a model that it depends on has
* initialized, as dependency chains will not yet be established.
* XXX Does this really matter? Or matter much?
* @const
*/
var PRIORITY_INIT_DELTA = 5000;
function identity(x) { return x; }
/** @const */
var noop = identity;
function isfunction (x) {
return typeof x === 'function';
}
function isString(x) {
return typeof x === 'string';
}
/**
* Returns a function that returns the elapsed time.
* @return {function(): Number} Function that returns elapsed time.
*/
function timer() {
var start = new Date().getTime();
/**
* Function that returns elapsed time since the outer function was invoked.
* @return {Number} Elapsed time in ms
*/
return function () {
return new Date().getTime() - start;
};
}
function warn() {
if (TBONE_DEBUG) {
console.warn.apply(console, arguments);
}
}
function error() {
if (TBONE_DEBUG) {
console.error.apply(console, arguments);
}
}
/** @const */
var ERROR = 1;
/** @const */
var WARN = 2;
/** @const */
var INFO = 3;
/** @const */
var VERBOSE = 4;
var logLevels = {
type: {
},
context: {
},
event: {
},
base: WARN
};
if (TBONE_DEBUG) {
tbone['watchLog'] = function (name, level) {
if (level == null) { level = VERBOSE; }
logLevels.type[name] = VERBOSE;
logLevels.context[name] = VERBOSE;
logLevels.event[name] = VERBOSE;
};
}
var events = [];
var viewRenders = 0;
/**
* Dynamic counter of how many ajax requests are inflight.
* @type {Number}
*/
var inflight = 0;
tbone['isReady'] = function () {
return !inflight && !schedulerQueue.length;
};
var logCallbacks = [];
function log () {
if (TBONE_DEBUG) {
for (var i = 0; i < logCallbacks.length; i++) {
logCallbacks[i].apply(this, arguments);
}
}
}
/**
* Log an event. The event is piped to the JS console if the level is less than or equal to the
* matched maximum log level based on the logLevels configuration above.
* @param {Number} level Log level: 1=error, 2=warn, 3=info, 4=verbose
* @param {string|Backbone.Model|Backbone.View|Scope} context What is logging this event
* @param {string} event Short event type string
* @param {string|Object} msg Message string with tokens that will be
* rendered from data. Or just relevant data.
* @param {Object=} data Relevant data
*/
function logconsole (level, context, event, msg, data) {
var name = isString(context) ? context : context.name;
var type = (isString(context) ? context :
context.isModel ? 'model' :
context.isView ? 'view' :
context.isScope ? 'scope' : '??');
var threshold = Math.max(logLevels.context[name] || 0,
logLevels.event[event] || 0,
logLevels.type[type] || 0) || logLevels.base;
if (event === 'lookups') {
msg = _.reduce(msg, function(memo, map, id) {
memo[map.__path__] = map;
return memo;
}, {});
}
if (level <= threshold) {
/**
* If a msg is a string, render it as a template with data as the data.
* If msg is not a string, just output the data below.
*/
var templated = isString(msg) ? _.template(msg, data || {}) : '';
var includeColon = !!templated || !!msg;
var frame = type === name ? type : (type + ' ' + name);
var message = frame + ' / ' + event + (includeColon ? ': ' : '');
var logfn = console[(level === ERROR ? 'error' : level === WARN ? 'warn' : 'log')];
logfn.call(console, message, templated || msg || '');
}
}
function onLog (cb) {
logCallbacks.push(cb);
}
if (TBONE_DEBUG) {
tbone['onLog'] = onLog;
onLog(logconsole);
}
/**
* Returns the list of unique listeners attached to the specified model/view.
* @param {Backbone.Model|Backbone.View} self
* @return {Array.<Backbone.Model|Backbone.View|Scope>} array of listeners
*/
function getListeners(self) {
var listeners = [];
_.each(_.values(self['_callbacks'] || {}), function (ll) {
var curr = ll.next;
while (true) {
if (curr.context) {
listeners.push(curr.context);
curr = curr.next;
} else {
break;
}
}
});
return _.uniq(listeners);
}
//
/**
* Returns true if there is a view that is listening (directly or indirectly)
* to this model. Useful for determining whether the current model should
* be updated (if a model is updated in the forest and nobody is there to
* hear it, then why update it in the first place?)
* @param {Backbone.Model|Backbone.View} self
* @return {Boolean}
*/
function hasViewListener(self) {
var todo = [];
var usedModels = {};
todo.push(self);
usedModels[self.name] = true;
while (todo.length) {
var next = todo.pop();
var listeners = getListeners(next);
for (var i = 0; i < listeners.length; i++) {
var listener = listeners[i];
if (listener.isScope) {
// The listener context is the model or view to whom the scope belongs.
// Here, we care about that model/view, not the scope, because that's
// what everyone else might be listening to.
listener = listener.context;
}
// listener might be undefined right now if the scope above didn't have a context.
if (listener) {
if (listener.isView) {
// We found a view that depends on the original model!
return true;
}
// listener could also have been a scope with a context that was neither
// a model nor a view.
if (listener.isModel) {
var name = listener['name'];
if (name && !usedModels[listener.name]) {
todo.push(listener);
usedModels[name] = true;
}
}
}
}
}
return false;
}
/**
* currentParentScope globally tracks the current executing scope, so that subscopes
* created during its execution (i.e. by tbone.autorun) can register themselves as
* subscopes of the parent (this is important for recursive destruction of scopes).
*/
var currentParentScope;
/**
* An autobinding function execution scope. See autorun for details.
* @constructor
*/
function Scope(fn, context, priority, name, onExecuteCb, onExecuteContext) {
_.extend(this, {
fn: fn,
context: context,
priority: priority,
name: name,
onExecuteCb: onExecuteCb,
onExecuteContext: onExecuteContext,
subScopes: []
});
}
_.extend(Scope.prototype,
/** @lends {Scope.prototype} */ {
/**
* Used to identify that an object is a Scope
* @type {Boolean}
*/
isScope: true,
/**
* Queue function execution in the scheduler
*/
trigger: function () {
queueExec(this);
},
/**
* Execute the wrapped function, tracking all values referenced through lookup(),
* and binding to those data sources such that the function is re-executed whenever
* those values change. Each execution re-tracks and re-binds all data sources; the
* actual sources bound on each execution may differ depending on what is looked up.
*/
execute: function () {
var self = this;
if (!self.destroyed) {
self.unbindAll();
self.destroySubScopes();
// Save our parent's lookups and subscopes. It's like pushing our own values
// onto the top of each stack.
var oldLookups = recentLookups;
this.lookups = recentLookups = {};
var oldParentScope = currentParentScope;
currentParentScope = self;
// ** Call the payload function **
// This function must be synchronous. Anything that is looked up using
// tbone.lookup before this function returns (that is not inside a subscope)
// will get bound below.
self.fn.call(self.context);
_.each(recentLookups, function (propMap) {
var obj = propMap['__obj__'];
if (obj.isCollection) {
/**
* This is not as efficient as it could be.
*/
obj.on('add remove reset', self.trigger, self);
} else {
if (propMap['*']) {
obj.on('change', self.trigger, self);
} else {
for (var prop in propMap) {
if (prop !== '__obj__' && prop !== '__path__') {
obj.on('change:' + prop, self.trigger, self);
}
}
}
}
});
// This is intended primarily for diagnostics. onExecute may either be a
// function, or an array with a function and a context to use for the
// function call. In either case, this Scope is passed as the only argument.
if (self.onExecuteCb) {
self.onExecuteCb.call(self.onExecuteContext, this);
}
// Pop our own lookups and parent scope off the stack, restoring them to
// the values we saved above.
recentLookups = oldLookups;
currentParentScope = oldParentScope;
}
},
/**
* For each model which we've bound, tell it to unbind all events where this
* scope is the context of the binding.
*/
unbindAll: function () {
var self = this;
_.each(this.lookups || {}, function (propMap) {
propMap['__obj__'].off(null, null, self);
});
},
/**
* Destroy any execution scopes that were creation during execution of this function.
*/
destroySubScopes: function () {
_.each(this.subScopes, function (subScope) {
subScope.destroy();
});
this.subScopes = [];
},
/**
* Destroy this scope. Which means to unbind everything, destroy scopes recursively,
* and ignore any execute calls which may already be queued in the scheduler.
*/
destroy: function () {
this.destroyed = true;
this.unbindAll();
this.destroySubScopes();
}
});
/**
* tbone.autorun
*
* Wrap a function call with automatic binding for any model properties accessed
* during the function's execution.
*
* Models and views update automatically by wrapping their reset functions with this.
*
* Additionally, this can be used within postRender callbacks to section off a smaller
* block of code to repeat when its own referenced properties are updated, without
* needing to re-render the entire view.
* @param {Function} fn Function to invoke
* @param {Backbone.Model|Backbone.View} context Context to pass on invocation
* @param {number} priority Scheduling priority - higher = sooner
* @param {string} name Name for debugging purposes
* @return {Scope} A new Scope created to wrap this function
*/
function autorun(fn, context, priority, name, onExecuteCb, onExecuteContext, detached) {
// Default priority and name if not specified. Priority is important in
// preventing unnecessary refreshes of views/subscopes that may be slated
// for destruction by a parent; the parent should have priority so as
// to execute first.
if (!priority) {
priority = currentParentScope ? currentParentScope.priority - 1 : 0;
}
if (!name) {
name = currentParentScope ? currentParentScope.name + '+' : 'unnamed';
}
// Create a new scope for this function
var scope = new Scope(fn, context, priority, name, onExecuteCb, onExecuteContext);
// If this is a subscope, add it to its parent's list of subscopes.
if (!detached && currentParentScope) {
currentParentScope.subScopes.push(scope);
}
// Run the associated function (and bind associated models)
scope.execute();
// Return the scope object; this is used by BaseView to destroy
// scopes when the associated view is destroyed.
return scope;
}
/**
* Generate and return a unique identifier which we attach to an object.
* The object is typically a view, model, or scope, and is used to compare
* object references for equality using a hash Object for efficiency.
* @param {Object} obj Object to get id from ()
* @return {string} Unique ID assigned to this object
*/
function uniqueId(obj) {
return obj['tboneid'] = obj['tboneid'] || nextId++;
}
var nextId = 1;
/**
* List of Scopes to be executed immediately.
* @type {Array.<Scope>}
*/
var schedulerQueue = [];
/**
* Flag indicating that the schedulerQueue is unsorted.
* @type {Boolean}
*/
var dirty;
/**
* Hash map of all the current Scope uniqueIds that are already
* scheduled for immediate execution.
* @type {Object.<string, Boolean>}
*/
var scopesQueued = {};
/**
* Pop the highest priority Scope from the schedulerQueue.
* @return {Scope} Scope to be executed next
*/
function pop() {
/**
* The schedulerQueue is lazily sorted using the built-in Array.prototype.sort.
* This is not as theoretically-efficient as standard priority queue algorithms,
* but Array.prototype.sort is fast enough that this should work well enough for
* everyone, hopefully.
*/
if (dirty) {
schedulerQueue.sort(function (a, b) {
/**
* TODO for sync models, use dependency graph in addition to priority
* to order execution in such a way as to avoid immediate re-execution.
*/
return a.priority - b.priority;
});
dirty = false;
}
return schedulerQueue.pop();
}
/**
* Flag indicating whether a processQueue timer has already been set.
*/
var processQueueTimer;
/**
* Queue the specified Scope for execution if it is not already queued.
* @param {Scope} scope
*/
function queueExec (scope) {
var contextId = uniqueId(scope);
if (!scopesQueued[contextId]) {
scopesQueued[contextId] = true;
/**
* Push the scope onto the queue of scopes to be executed immediately.
*/
schedulerQueue.push(scope);
/**
* Mark the queue as dirty; the priority of the scope we just added
* is not immediately reflected in the queue order.
*/
dirty = true;
/**
* If a timer to process the queue is not already set, set one.
*/
if (!processQueueTimer && unfrozen) {
processQueueTimer = _.defer(processQueue);
}
}
}
var unfrozen = true;
/**
* Drain the Scope execution queue, in priority order.
*/
function processQueue () {
processQueueTimer = null;
var queueProcessTime = timer();
var scope;
while (unfrozen && !!(scope = pop())) {
/**
* Update the scopesQueued map so that this Scope may be requeued.
*/
delete scopesQueued[uniqueId(scope)];
var scopeExecTime = timer();
/**
* Execute the scope, and in turn, the wrapped function.
*/
scope.execute();
log(VERBOSE, 'scheduler', 'exec', '<%=priority%> <%=duration%>ms <%=name%>', {
'priority': scope.priority,
'name': scope.name,
'duration': scopeExecTime()
});
}
log(VERBOSE, 'scheduler', 'processQueue', 'ran for <%=duration%>ms', {
'duration': queueProcessTime()
});
log(VERBOSE, 'scheduler', 'viewRenders', 'rendered <%=viewRenders%> total', {
'viewRenders': viewRenders
});
}
/**
* Drain to the tbone processQueue, processing all scope executes immediately.
* This is useful both for testing and MAYBE also for optimizing responsiveness by
* draining at the end of a keyboard / mouse event handler.
*/
tbone['drain'] = function () {
if (processQueueTimer) {
clearTimeout(processQueueTimer);
}
processQueue();
};
tbone['freeze'] = function () {
unfrozen = false;
};
/**
* baseModel
* @constructor
* @extends Backbone.Model
*/
var baseModel = Backbone.Model.extend({
isModel: true,
backboneBasePrototype: Backbone.Model.prototype,
/**
* Constructor function to initialize each new model instance.
* @return {[type]}
*/
initialize: function () {
var self = this;
uniqueId(self);
var isAsync = self.sleeping = self.isAsync();
var priority = isAsync ? BASE_PRIORITY_MODEL_ASYNC : BASE_PRIORITY_MODEL_SYNC;
/**
* Queue the autorun of update. We want this to happen after the current JS module
* is loaded but before anything else gets updated. We can't do that with setTimeout
* or _.defer because that could possibly fire after processQueue.
*/
queueExec({
execute: function () {
self.scope = autorun(self.update, self, priority, 'model_' + self.name,
self.onScopeExecute, self);
},
priority: priority + PRIORITY_INIT_DELTA
});
},
/**
* Indicates whether this function should use the asynchronous or
* synchronous logic.
* @return {Boolean}
*/
isAsync: function () {
return !!this['_url'];
},
onScopeExecute: function (scope) {
log(INFO, this, 'lookups', scope.lookups);
},
/**
* Triggers scope re-execution.
*/
reset: function () {
if (this.scope) {
this.scope.trigger();
}
},
'isVisible': function () {
return hasViewListener(this);
},
update: function () {
var self = this;
if (self.isAsync()) {
self.updateAsync();
} else {
self.updateSync();
}
},
updateAsync: function () {
var self = this;
var expirationSeconds = self['expirationSeconds'];
function complete() {
inflight--;
delete self.__xhr;
if (expirationSeconds) {
if (self.expirationTimeout) {
clearTimeout(self.expirationTimeout);
}
self.expirationTimeout = setTimeout(function () {
self.reset();
}, expirationSeconds * 1000);
}
}
var url = self.url();
var lastFetchedUrl = self.fetchedUrl;
self.sleeping = !this['isVisible']();
if (self.sleeping) {
/**
* Regardless of whether url is non-null, this model goes to sleep
* if there's no view listener waiting for data (directly or through
* a chain of other models) from this model.
**/
log(INFO, self, 'sleep');
self.sleeping = true;
} else if (url != null && (expirationSeconds || url !== lastFetchedUrl)) {
/**
* If a defined URL function returns null, it will prevent fetching.
* This can be used e.g. to prevent loading until all required
* parameters are set.
**/
self.fetchedUrl = url;
self.clear();
inflight++;
self.fetch({
'dataType': 'text',
success: function () {
self['postFetch']();
self.trigger('fetch');
log(INFO, self, 'updated', self.toJSON());
complete();
},
error: function () {
complete();
},
'beforeSend': function (xhr) {
// If we have an active XHR in flight, we should abort
// it because we don't want that anymore.
if (self.__xhr) {
log(WARN, self, 'abort',
'aborting obsolete ajax request. old: <%=oldurl%>, new: <%=newurl%>', {
'oldurl': lastFetchedUrl,
'newurl': url
});
self.__xhr.abort();
}
self.__xhr = xhr;
xhr['__backbone__'] = true;
},
url: url
});
}
},
updateSync: function () {
var self = this;
// this.state returns the new state, synchronously
var newParams = self['state']();
if (newParams === null) {
log(VERBOSE, self, 'update cancelled');
return;
}
lookup.call(self, '__self__', newParams);
log(INFO, self, 'updated', self.toJSON());
},
'state': identity,
'postFetch': noop
});
/**
* Create a new model type.
* @param {string} name Model name
* @param {Backbone.Model|Function=} base Parent model -- or state function of simple sync model
* @param {Object.<string, Object>=} opts Properties to override (optional)
* @return {Backbone.Model}
*/
function createModel(name, base, opts) {
if (TBONE_DEBUG && !isString(name)) {
throw 'createModel requires name parameter';
}
/**
* If only a name is provided, this is a passive model. Disable autorun so that this model
* will only be updated by set() calls. This is useful in building simple dynamic data
* sources for other models.
*/
if (!base) {
opts = {
initialize: noop
};
base = baseModel;
/**
* If the second parameter is a function, use it as the state function of a simple sync model.
*/
} else if (!base['__super__']) {
opts = {
'state': base
};
base = baseModel;
}
opts = _.extend({
name: name
}, opts || {});
var model = models[name] = base.extend(opts);
var modelPrototype = model.prototype;
_.extend(model, /** @lends {model} */ {
/**
* Create and return an instance of this model using the model name as the instance name.
* @return {Backbone.Model}
*/
'singleton': function () {
return this['make'](name);
},
/**
* Create and return an instance of this model at tbone.data[instanceName].
* @return {Backbone.Model}
*/
'make': function (instanceName) {
var instance = new model();
if (instanceName) {
var nameParts = instanceName.split('.');
var _data = data;
_.each(nameParts.slice(0, nameParts.length - 1), function (part) {
_data = _data[part] = _data[part] || {};
});
_data[nameParts[nameParts.length - 1]] = instance;
}
return instance;
}
});
return model;
}
var baseCollection = Backbone.Collection.extend({
isCollection: true,
backboneBasePrototype: Backbone.Collection.prototype
});
function createCollection(name, model) {
if (TBONE_DEBUG && !isString(name)) {
throw 'createCollection requires name parameter';
}
var opts = {
name: name,
model: model || baseModel
};
var collection = collections[name] = baseCollection.extend(opts);
// XXX this is basically the same as in createModel. Unify.
var collectionPrototype = collection.prototype;
_.extend(collection, /** @lends {collection} */ {
'singleton': function () {
return this['make'](name);
},
'make': function (instanceName) {
var instance = new collection();
if (instanceName) {
var nameParts = instanceName.split('.');
var _data = data;
_.each(nameParts.slice(0, nameParts.length - 1), function (part) {
_data = _data[part] = _data[part] || {};
});
_data[nameParts[nameParts.length - 1]] = instance;
}
return instance;
}
});
return collection;
}
var global = window;
var recentLookups;
/**
* "Don't Get Data" - Special flag for lookup to return the model/collection instead
* of calling toJSON() on it.
* @const
*/
var DONT_GET_DATA = 1;
/**
* "Iterate Over Models" - Special flag for lookup to return an iterator over the
* models of the collection, enabling iteration over models, which is what we want
* to do when using _.each(collection ...) in a template, as this allows us to
* use model.lookup(...) and properly bind references to the models.
* @const
*/
var ITERATE_OVER_MODELS = 2;
/**
* "Extend on set" - instead of replacing an entire object or model's values on
* set, extend that object/model instead.
* @const
*/
var EXTEND_ON_SET = 3;
function lookup(flag, query, value) {
var isSet;
var dontGetData = flag === DONT_GET_DATA;
var iterateOverModels = flag === ITERATE_OVER_MODELS;
var extendOnSet = flag === EXTEND_ON_SET;
if (!dontGetData && !iterateOverModels && !extendOnSet) {
/**
* If no flag provided, shift the query and value over. We do it this way instead
* of having flag last so that we can type-check flag and discern optional flags
* from optional values. And flag should only be used internally, anyway.
*/
value = query;
query = flag;
flag = null;
/**
* Use arguments.length to switch to set mode in order to properly support
* setting undefined.
*/
if (arguments.length === 2) {
isSet = true;
}
} else if (extendOnSet) {
isSet = true;
}
var args = query.split('.');
var setprop;
if (isSet) {
/**
* For set operations, we only want to look up the parent of the property we
* are modifying; pop the final property we're setting from args and save it
* for later.
* @type {string}
*/
setprop = args.pop();
}
/**
* If this function was called with a bindable context (i.e. a Model or Collection),
* then use that as the root data object instead of the global tbone.data.
*/
var _data = (!this || !this['isBindable']) ? data : this;
var name_parts = [];
var myRecentLookup = {};
var propAfterRecentLookup;
var id;
var arg;
var foundBindable;
if (_data['isBindable']) {
id = uniqueId(_data);
foundBindable = true;
myRecentLookup = (recentLookups && recentLookups[id]) || {
'__obj__': _data
};
if (recentLookups) {
recentLookups[id] = myRecentLookup;
}
}
while ((arg = args.shift()) != null && arg !== '__self__') {
name_parts.push(arg);
if (_data['isBindable']) {
foundBindable = true;
if (_data.isModel) {
_data = _data.get(arg);
} else if (_data.isCollection) {
// XXX should we support .get() for collections? e.g. IDs starting with #?
myRecentLookup[arg] = _data = _data.at(arg);
}
if (!propAfterRecentLookup) {
propAfterRecentLookup = arg;
myRecentLookup[arg] = _data;
}
} else {
_data = _data[arg];
}
if (_data == null) {
/**
* This is not right to do in the case of a deep set where the structure
* is not created yet. We might want to implicitly do a mkdir -p to support
* this, e.g. T('some.deep.random.property.to.set', value)
* -> { some: { deep: { random: { property: { to: { set: value } } } } } }
*/
break;
} else if (_data['isBindable']) {
foundBindable = true;
id = uniqueId(_data);
myRecentLookup = (recentLookups && recentLookups[id]) || {
'__obj__': _data,
'__path__': name_parts.join('.') // XXX a model could exist at two paths]
};
if (recentLookups) {
recentLookups[id] = myRecentLookup;
}
propAfterRecentLookup = null;
}
}
/**
* If we haven't found a model / collection in the process of looking something up,
* log an error. A common mistake could be to attempt to read values before models
* are initialized.
**/
if (TBONE_DEBUG && !isSet && !foundBindable) {
log(ERROR, 'lookup', 'no bindable found',
'No model/collection found while looking up "<%=query%>".', {
query: query
});
}
if (_data) {
if (isSet) {
var currProp = (
query === '__self__' ? _data : // only useful if _data is a model
_data.isModel ? _data.get(setprop) :
_data.isCollection ? _data.at(setprop) :
_data[setprop]);
if (currProp && currProp.isModel) {
/**
* When setting to an entire model, we use different semantics; we want the
* values provided to be set to the model, not replace the model.
*/
if (value) {
/**
* Unless extendOnSet is set, remove any properties from the model that
* are not present in the value we're setting it to. Extend-semantics
* are made available to the user via tbone.extend.
*/
if (!extendOnSet) {
for (var k in currProp.toJSON()) {
if (value[k] === undefined) {
currProp.unset(k);
}
}
}
currProp.set(value);
} else {
currProp.clear();
}
} else if (currProp !== value) {
if (_data.isModel) {
/**
* Set the value to the top-level model property. Common case.
*/
_data.set(setprop, value);
} else if (_data.isCollection) {
// XXX What makes sense to do here?
} else if (_data[setprop] !== value) {
/**
* Set the value to a property on a regular JS object.
*/
_data[setprop] = value;
/**
* If we're setting a nested property of a model (or collection?), then
* trigger a change event for the top-level property.
*/
if (propAfterRecentLookup) {
myRecentLookup['__obj__'].trigger('change:' + propAfterRecentLookup);
}
}