-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathascot.js
1003 lines (805 loc) · 26 KB
/
ascot.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(e){if("function"==typeof bootstrap)bootstrap("scripts/index.js",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeScriptsindexjs=e}else"undefined"!=typeof window?window.scriptsindexjs=e():global.scriptsindexjs=e()})(function(){var define,ses,bootstrap,module,exports;
return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"ascot.Model":[function(require,module,exports){
module.exports=require('FFRxKb');
},{}],"ascot.EventEmitter":[function(require,module,exports){
module.exports=require('BvhrnU');
},{}],"ascot.DOMView":[function(require,module,exports){
module.exports=require('GirLh0');
},{}],"UI2WPJ":[function(require,module,exports){
'use strict';
/**
* The top-level ascot function. Creates new prototypes by mixing together an array of prototypes
* and applying an expanded descriptor that includes mixin modifiers.
* @param {Array} mixins An array of prototypes to mix in
* @param {Object} descriptor A property descriptor
* @return {Object} A new object prototype
*/
function ascot(/* arguments */) {
var mixins, descriptor, constructor, item;
// Establish appropriate arguments
if (arguments.length === 2) {
mixins = arguments[0];
descriptor = arguments[1];
} else {
mixins = [];
descriptor = arguments[0];
}
descriptor = descriptor || {};
// Collect each prototype's descriptor
for (var i=0, len=mixins.length; i<len; i+=1) {
item = mixins[i];
// Allow for string references to base ascot classes
item = mixins[i] = typeof item === 'string' ? ascot[item] : item;
mixins[i] = item.descriptor;
}
// Expand and add current descriptor to mixins
for (var j in descriptor) {
descriptor[j] = expandDescriptor(descriptor[j]);
}
mixins.push(descriptor);
descriptor = combineDescriptors(mixins);
// Form a new constructor
constructor = createConstructor(descriptor);
return constructor;
}
/******************
* Construction *
******************/
/**
* Creates a new constructor that may be used to create objects with the 'new' keyword
* @return {Function} A standard constructor function
*/
function createConstructor(descriptor) {
var constructor = (function(desc) {
return function(/* arguments */) {
/* jshint validthis : true */
Object.defineProperties(this, deepCopy(desc));
if (this.construct) { this.construct.apply(this, arguments); }
};
})(descriptor);
constructor.prototype = {};
constructor.descriptor = descriptor;
return constructor;
}
/*****************
* Descriptors *
*****************/
/**
* Expands a shorthand descriptor to a formal descriptor. A shorthand descriptor consists
* of three-character abbreviations of 'writable', 'configurable', etc. in the form :
* wrt, cfg, enm, val along with the normal get & set. Additionally, properties for which
* a property descriptor has not been set get a default descriptor.
* @param {Object} descriptor A shorthand descriptor
*/
function expandDescriptor(descriptor) {
var newDescriptor = {};
if (!descriptor) { return; }
// Expand the descriptor if the argument is a valid descriptor
if (isDescriptor(descriptor)) {
for (var i in descriptor) {
switch (i) {
case 'enm' :
newDescriptor.enumerable = descriptor[i];
break;
case 'cfg' :
newDescriptor.configurable = descriptor[i];
break;
case 'wrt' :
newDescriptor.writable = descriptor[i];
break;
case 'val' :
newDescriptor.value = descriptor[i];
break;
default :
newDescriptor[i] = descriptor[i];
break;
}
}
return newDescriptor;
}
// Create a default desciptor
else {
return {
writable : true,
enumerable : true,
configurable : true,
value : descriptor
};
}
}
/**
* Creates a new prototype from a set of property descriptor objects. The prototype
* is the result from a
* @param {Array} descriptors An array of expanded descriptors.
*/
function combineDescriptors(descriptors) {
var desc, appendedDesc, propName;
var newDescriptor = {};
for (var i=0, len=descriptors.length; i<len; i+=1) {
desc = descriptors[i];
for (var j in desc) {
appendedDesc = appendDescriptor(j, newDescriptor[j], desc[j]);
// Determine if assigning a value to an accessed property
newDescriptor[j] = appendedDesc === true ? newDescriptor[j] : appendedDesc;
// Assign value to accessed property
if (appendedDesc === true) {
propName = '_' + j;
newDescriptor[propName] = newDescriptor[propName] || {};
newDescriptor[propName].value = desc[j].value;
}
}
}
return newDescriptor;
}
/**
* Appends a descriptor to a target descriptor
* @param {String} propertyName The name of the property associated with this descriptor
* @param {Object} target A target descriptor to append to
* @param {Object} descriptor An expanded descriptor including mixin modifiers
*/
function appendDescriptor(propertyName, target, descriptor) {
var modifier;
var isNew = !target;
target = target || {};
// Return true if this is an implicit accessor value override
if ((target.get || target.set) && (descriptor.value)) {
return true;
}
// Extract modifiers and copy over new descriptor properties
for (var i in descriptor) {
// Retain mixin modifiers
if (i.indexOf('$') >= 0) {
modifier = {};
modifier.key = i;
modifier.value = target[i] = descriptor[i];
}
// Copy over normal descriptor properties
else {
target[i] = deepCopy(descriptor[i]);
}
}
// OK to apply modifiers
if (modifier) {
applyModifier(propertyName, target, modifier);
}
// Always allow overwriting of notational private variables
else if (propertyName.indexOf('_') === 0) {
return target;
}
// Don't allow inadvertant overrides
else if (!modifier && !isNew) {
throw new Error('Attempted to overwrite an existing property without a modifier. Apply a modifier or use $override.');
}
return target;
}
/*********************
* Mixin Modifiers *
*********************/
/**
* Applies a modifier to a descriptor, creating appropriate iterators or appending/prepending
* to existing methods.
* @param {String} propertyName The name of the property associated with this descriptor
* @param {Objects} descriptor A target descriptor to modify
* @param {Object} modifier A key and value describing a particular modifier
*/
function applyModifier(propertyName, descriptor, modifier) {
var calls;
var val = descriptor.value;
switch (modifier.key) {
case '$chain' :
calls = processCalls(propertyName, modifier.value);
descriptor.value = createChain(calls);
break;
case '$iterate' :
calls = processCalls(propertyName, modifier.value);
descriptor.value = createIterator(calls);
break;
case '$before' :
descriptor.value = prependIterator(val, modifier.value);
break;
case '$after' :
descriptor.value = appendIterator(val, modifier.value);
break;
case '$override' :
applyOverride(descriptor, modifier.value);
break;
default :
break;
}
return descriptor;
}
/**
* Processes passed calls from a iterator property descriptor. If an item is a
* constructor, a function of the given name is sought on a descriptor and used instead.
* @param {String} name The name of the method to iterate
* @param {Array} items Objects and functions composing the iterator
* @return {Array} The new iterator
*/
function processCalls(name, items) {
var item;
var calls = [];
// Add each item to the iterator
for (var i=0, len=items.length; i<len; i+=1) {
item = items[i];
if (!item) { continue; }
// Seek a function within a prototype and add to the iterator
if (item.descriptor && typeof item.descriptor[name].value === 'function') {
calls.push(item.descriptor[name].value);
}
// Add functions to the iterator directly
else if (typeof item === 'function') {
calls.push(item);
}
}
return calls;
}
/**
* Creates and returns a chaining iterator
* @param {Array} calls A list of calls associated with the iterator
*/
function createChain(calls) {
// Create the iterator method that chains through each call
function iterator() {
/* jshint validthis : true */
var args = Array.prototype.slice.call(arguments, 0);
var calls = iterator._calls;
for (var j=0, jLen=calls.length; j<jLen; j+=1) {
args[0] = calls[j].apply(this, args);
}
return args[0];
}
iterator._calls = calls;
return iterator;
}
/**
* Creates and returns a chaining iterator
* @param {Array} calls A list of calls associated with the iterator
*/
function createIterator(calls) {
// Create the iterator method that chains through each call
function iterator() {
/* jshint validthis : true */
var val;
var args = Array.prototype.slice.call(arguments, 0);
var calls = iterator._calls;
for (var j=0, jLen=calls.length; j<jLen; j+=1) {
val = calls[j].apply(this, args);
}
return val;
}
iterator._calls = calls;
return iterator;
}
/**
* Prepends a function to an existing iterator. Creates an iterator if one had not
* yet been created.
* @param {Function} iterator An existing iterator function
* @param {Function} fn A function to append
* @return {Function} iterator
*/
function prependIterator(iterator, fn) {
var calls = Array.prototype.slice.call(iterator._calls, 0);
if (typeof iterator !== 'function') {
return fn;
}
// Prepend to an existing iterator
if (calls) {
calls.splice(0, 0, fn);
iterator._calls = calls;
}
// Create a new iterator if one had not been created
else {
iterator = createIterator([fn, iterator]);
}
return iterator;
}
/**
* Appends a function to an existing iterator. Creates an iterator if one had not
* yet been created.
* @param {Function} iterator An existing iterator function
* @param {Function} fn A function to append
* @return {Function} iterator
*/
function appendIterator(iterator, fn) {
var calls = Array.prototype.slice.call(iterator._calls, 0);
if (typeof iterator !== 'function') {
return fn;
}
// Prepend to an existing iterator
if (calls) {
calls.push(fn);
iterator._calls = calls;
}
// Create a new iterator if one had not been created
else {
iterator = createIterator([iterator, fn]);
}
return iterator;
}
/**
* Applies the appropriate override. Accessor properties may be overridden
* by specifying $override : true, whereas data properties have their values overridden
* by $override : newValue
* @param {Object} descriptor The descriptor to apply the override to
* @param {Variant} override A function listed under descriptor.value
*/
function applyOverride(descriptor, override) {
// Only modify values for data properties
if (!descriptor.get && !descriptor.set) {
descriptor.value = override;
}
}
/***************
* Utilities *
***************/
/**
* Determines if an object is a descriptor
* @param {Object} obj A proposed descriptor
*/
function isDescriptor(obj) {
if (!obj || obj !== Object(obj)) { return false; }
if (
'enm' in obj ||
'cfg' in obj ||
'wrt' in obj ||
'val' in obj ||
'enumerable' in obj ||
'configurable' in obj ||
'writable' in obj ||
'value' in obj ||
'get' in obj ||
'set' in obj ||
'$chain' in obj ||
'$iterate' in obj ||
'$before' in obj ||
'$after' in obj ||
'$override' in obj
)
{ return true; }
return false;
}
/**
* Copies the passed item, regardless of data type. Objects and arrays are
* copied by value and not by reference.
* @param {Variant} item Something to copy
*/
function deepCopy(item) {
var copy;
// Recursively copy arrays
if (Array.isArray(item)) {
copy = [];
for (var i=0, len=item.length; i<len; i+=1) {
copy.push(deepCopy(item[i]));
}
return copy;
}
// Recursively copy objects
else if (item === Object(item) && typeof item !== 'function') {
copy = {};
for (var j in item) {
copy[j] = deepCopy(item[j]);
}
return copy;
}
// Just return the value
return item;
}
/*************
* Exports *
*************/
module.exports = ascot;
},{}],"GirLh0":[function(require,module,exports){
'use strict';
var ascot = require('ascot.main');
var EventEmitter = require('ascot.EventEmitter');
/**
* Constructs the DOMView, establishing its data and template and performing
* an initial rendering.
* @param {Variant} data The data associated with this view
* @param {Function} template An HTML templating function
*/
function construct(data, template) {
this._data = data || this._data;
this.template = template || this.template;
if (data) { bindViewToModel.call(this); }
render.call(this);
return this;
}
/**
* Renders the DOMView using the available template. On rendering, a new element is created,
* and must be added to the DOM.
*/
function render() {
var div = document.createElement('div');
div.innerHTML = this.template(this.data);
this._element = div.firstChild;
}
/*************
* Handles *
*************/
/**
* Establishes accessors to specific elements or sets of elements within this view.
* Handles are set using a hash map that associates handles with DOM query selector strings.
* @param {Object} handles A hash map of handles
*/
function setHandles(handles) {
var _handles = this._handles;
for (var i in handles) {
Object.defineProperty(this, i, {
get : getElementBySelector.bind(this, handles[i]),
enumerable : true,
configurable : true
});
_handles[i] = handles[i];
}
}
/**
* Returns a set of current handles
*/
function getHandles() {
return this._handles;
}
/**
* Gets a single element by query selector. The element retrieved is relative
* to this view's element.
* @param {String} selector A query selector string
*/
function getElementBySelector(selector) {
var el = this._element;
return el.querySelector(selector);
}
/******************
* Data Binding *
******************/
/**
* Binds the view to its model. Whenever a model changes, it triggers a callback
* that updates the view accordingly.
*/
function bindViewToModel() {
var model = this.data;
var listener = this._modelBindListener = this._modelBindListener || updateView.bind(this);
if (model.on) {
model.on('load', listener);
model.on('change', listener);
}
}
/**
* Unbinds the view from its current model by removing its event listeners
*/
function unbindViewFromModel() {
var model = this.data;
var listener = this._modelBindListener;
if (!listener) { return; }
if (model.on) {
model.off('load', listener);
model.off('change', listener);
}
}
/**
* Updates the view, either by calling an update() method or triggering a
* re-rendering of the template.
* @param {Object} data The data used to update the view
* @param {String} path A period-delimited path to the data being modified
*/
function updateView(data, path) {
var el = this._element;
var parent = el.parentNode;
// Use update methods if available
if (this.update) { this.update(data, path); }
// Otherwise, re-render using a template and swap elements
else if (this.template) {
render.call(this);
if (parent) { parent.replaceChild(this._element, el); }
}
}
/***************
* Accessors *
***************/
/**
* Sets the view's data, updating the view accordingly
* @param {Variant} data The data associated with the view
*/
function setData(data) {
unbindViewFromModel.call(this);
this._data = data;
bindViewToModel.call(this);
updateView.call(this, data);
}
/**
* Gets the current view's data property
*/
function getData() {
return this._data;
}
/**
* Returns the view's top-level element
*/
function getElement() {
return this._element;
}
/**
* Returns the template associated with this view
*/
function getTemplate() {
return this._template;
}
/**
* Sets the template associated with this view
* @param {Function} template A templating function
*/
function setTemplate(template) {
this._template = template;
}
/*********
* API *
*********/
var api = {
construct : { val : construct, wrt : false, enm : false, cfg : false },
data : { get : getData, set : setData, enm : true, cfg : true },
_data : { val : null, wrt : true, enm : false, cfg : false },
element : { get : getElement, enm : true, cfg : false },
_element : { val : null, wrt : true, enm : false, cfg : false },
template : { get : getTemplate, set : setTemplate, enm : true, cfg : false },
_template : { val : null, wrt : true, enm : false, cfg : false },
// Handles
handles : { get : getHandles, set : setHandles, enm : true, cfg : true },
_handles : { val : {}, wrt : true, enm : false, cfg : false },
/* Override */
update : { val : null, wrt : true, enm : false, cfg : false }
};
/*************
* Exports *
*************/
ascot.DOMView = ascot([EventEmitter], api);
module.exports = ascot.DOMView;
},{"ascot.EventEmitter":"BvhrnU","ascot.main":"UI2WPJ"}],"BvhrnU":[function(require,module,exports){
'use strict';
var ascot = require('ascot.main');
/**
* Registers an event listener on the specified target
* @param {String} eventName The name of the event
* @param {Function} cb The new callback to handle the event
*/
function on(eventName, cb) {
var callbacks = this.eventListeners[eventName] = this.eventListeners[eventName] || [];
// Do nothing if a callback has already been added
if (callbacks.indexOf(cb) >= 0) { return; }
// Add the callback to the list of callbacks
callbacks.push(cb);
}
/**
* Registers an event listener on the specified target
* @param {String} eventName The name of the event
* @param {Function} cb The new callback to handle the event
*/
function off(eventName, cb) {
var index;
var callbacks = this.eventListeners[eventName] = this.eventListeners[eventName] || [];
// Remove the callback from the list
index = callbacks.indexOf(cb);
if (index >= 0) { callbacks.splice(index, 1); }
}
/**
* Removes all event listeners for a particular event from the emitter
*/
function removeAllListeners(eventName) {
if (eventName) {
this.eventListeners[eventName] = [];
} else {
this.eventListeners = {};
}
}
/**
* Emits the specified event, calling and passing the optional argument to all listeners
* @param {String} eventName The name of the event to emit
* @param {Variant} arg Any argument to pass to the event listeners
*/
function emit(eventName) {
var args = Array.prototype.slice.call(arguments, 0);
var callbacks = this.eventListeners[eventName] = this.eventListeners[eventName] || [];
args.shift();
for (var i=0, len=callbacks.length; i<len; i+=1) {
callbacks[i].apply(this, args);
}
}
/*********
* API *
*********/
var api = {
on : on,
off : off,
removeAllListeners : removeAllListeners,
emit : { val : emit, wrt : false, enm : false, cfg : false },
eventListeners : { val : {}, wrt : true, enm : false, cfg : false }
};
/*************
* Exports *
*************/
ascot.EventEmitter = ascot(api);
module.exports = ascot.EventEmitter;
},{"ascot.main":"UI2WPJ"}],"FFRxKb":[function(require,module,exports){
'use strict';
var ascot = require('ascot.main');
var EventEmitter = require('ascot.EventEmitter');
/****************
* Properties *
****************/
/**
* Whether to always attempt updating from the online location rather than retreive
* from localStorage
* @type {Boolean}
*/
var preferOnline = false;
/**
* The remote location of the data source for retrieval using XMLHttpRequest
* @type {String}
*/
var src = null;
/**
* Whether to store and retrieve this model from local storage
* @type {Boolean}
*/
var storeLocal = true;
/******************
* Construction *
******************/
/**
* Constructs the model, establishing and loading its data source.
* @param {String} src The data source associated with this model
*/
function construct(src) {
if (src) { this.load(src); }
}
/**********************************
* Loading, Storing, Retrieving *
**********************************/
/**
* Stores the model to local storage. Stored as a key/value pair where
* the key is the src of the data and the value is a JSON string.
*/
function store() {
localStorage[src] = JSON.stringify(this);
}
/**
* Loads the data either from a server or from local storage depending on settings and
* online status
* @param {String} src Optionally specify the source of the data
*/
function load(src) {
this.src = src || this.src;
if (localStorage[src] && !this.preferOnline) {
setTimeout(loadLocalData.bind(this), 0);
} else {
loadRemoteData.call(this);
}
}
/**
* Parses a json string and merges data with this model
* @param {String} json
*/
function loadLocalData() {
var localData = localStorage[this.src];
if (localData) { parse.call(this, localData); }
this.emit('load', this);
}
/**
* Parses passed json data
* @param {String} json A valid JSON string
*/
function parse(json) {
var data = JSON.parse(json);
// Performs optional processing steps to modify the structure of the data
if (this.process) { data = this.process(data); }
for (var i in data) { this[i] = data[i]; }
}
/**
* Loads data from the server. If the request fails, attempts loading data from localStorage.
*/
function loadRemoteData() {
var src = this.src;
var xhr = new XMLHttpRequest();
xhr.open('GET', src);
xhr.onreadystatechange = handleXHRResponse.bind(this, xhr);
xhr.send(null);
}
/**
* Handles incoming XHR responses
*/
function handleXHRResponse(xhr) {
var type, text;
// Request was successful
if (xhr.readyState === 4 && xhr.status === 200) {
type = xhr.getResponseHeader('Content-Type');
// Make sure response is JSON
if (type.indexOf('json') >= 0) {
text = xhr.responseText;
// Parse and load
parse.call(this, text);
// Store data locally
if (this.storeLocal) { this.store(); }
this.emit('load', this);
}
// Request failed, attempt loading locally instead
} else if (xhr.readyState === 4 && xhr.status !== 200) {
loadLocalData.call(this);
}
}
/********************
* Data Accessors *
********************/
/**
* Resolves a path and returns relevant data
* @param {String} path A period-delimited path to some data
*/
function resolve(path) {
var value = this;
path = path.split('.');
for (var i=0, len=path.length; i<len; i+=1) {
value = value[path[i]];
}
return value;
}
/**
* Sets data on the model
* @param {String} path An path to a location within the data model
* @param {Object|Variant} data The new data
*/
function set(/* arguments */) {
var path, addr, data, target, key;
// Adjust for arguments
if (arguments.length === 2) {
path = arguments[0];
data = arguments[1];
} else {
data = arguments[0];
}
// Handle path-referenced data change
if (path) {
addr = path;
addr = addr.split('.');
key = addr.pop();
target = this;
for (var i=0, len=addr.length; i<len; i+=1) {
target = target[addr[i]];
}
target[key] = data;
}
// Handle full data change
else {
for (var j in data) {
this[j] = data[j];
}
}
this.emit('change', this, path);
}
/*********
* API *
*********/
var api = {
construct : construct,
storeLocal : { val : storeLocal, wrt : true, enm : false, cfg : false },
src : { val : src, wrt : true, enm : false, cfg : false },
preferOnline : { val : preferOnline, wrt : true, enm : false, cfg : false },
store : store,
load : load,
set : set,
process : null,
resolve : resolve
};
/*************
* Exports *
*************/
ascot.Model = ascot([EventEmitter], api);
module.exports = ascot.Model;
},{"ascot.EventEmitter":"BvhrnU","ascot.main":"UI2WPJ"}],8:[function(require,module,exports){
'use strict';
var ascot = require('ascot.main');
require('ascot.EventEmitter');
require('ascot.DOMView');
require('ascot.Model');
module.exports = ascot;
},{"ascot.DOMView":"GirLh0","ascot.EventEmitter":"BvhrnU","ascot.Model":"FFRxKb","ascot.main":"UI2WPJ"}],"ascot.main":[function(require,module,exports){
module.exports=require('UI2WPJ');