-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackbone.app.js
2080 lines (1768 loc) · 54.9 KB
/
backbone.app.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
/**
* @name backbone.app
* @author makesites
* Homepage: http://github.com/makesites/backbone-app
* Version: 1.0.0 (Mon, 01 May 2023 01:46:44 GMT)
* @license Apache License, Version 2.0
*/
(function (lib) {
//"use strict";
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
var deps = ['jquery', 'underscore', 'backbone'];
define('backbone.app', deps, lib); // give the module a name
} else if ( typeof module === "object" && module && typeof module.exports === "object" ){
// Expose as module.exports in loaders that implement CommonJS module pattern.
module.exports = lib;
} else {
// Browser globals
var Query = window.jQuery || window.Zepto || window.vQuery;
lib(Query, window._, window.Backbone, window.APP);
}
}(function ($, _, Backbone) {
//"use strict";
// better way to define global scope?
var window = this.window || {};
var APP = window.APP || false;
// stop processing if APP is already part of the namespace
if( !APP ) (function(_, Backbone) {
// defaults =
var defaults = {
require: false,
routePath: "app/controllers/",
autoLookup: true,
pushState: false
};
// App contructor
APP = function(){
// get config
var options = arguments[0] || {};
var callback = arguments[1] || function(){};
// extend default options
options.require = options.require || (typeof define === 'function' && define.amd);
options = utils.extend( defaults, options );
// find router
var router = false;
// check URIs
var path = ( window.location ) ? window.location.pathname.split( '/' ) : [""];
// FIX: discart the first item if it's empty
if ( path[0] === "" ) path.shift();
//
if( options.require ){
// use require.js
var routerDefault = options.routePath +"default";
if(typeof options.require == "string"){
router = options.require;
} else if( !options.autoLookup ){
// don't try to lookup the router
router = routerDefault;
} else {
router = options.routePath;
router += ( !_.isEmpty(path[0]) ) ? path[0] : "default";
}
if( typeof require !== "undefined" ){
require( [ router ], function( controller ){
if( controller ){
callback( controller );
}
}, function (err) {
//The errback, error callback
//The error has a list of modules that failed
var failed = err.requireModules && err.requireModules[0];
// what if there's no controller???
if( failed == router ){
// fallback to the default controller
require( [ routerDefault ], function( controller ){
callback( controller );
});
} else {
//Some other error. Maybe show message to the user.
throw err;
}
});
} else {
// assuming System.js
System['import'](router).then(function( Controller ){
if( Controller['default'] ){
callback( Controller['default'] );
}
})['catch'](function(e) {
// revert to the default router
System['import'](routerDefault).then(function( Controller ){
callback( Controller['default'] );
});
});
}
return APP;
} else {
// find a router based on the path
for(var i in path ){
// discart the first item if it's empty
if( path[i] === "") continue;
router = (path[i].charAt(0).toUpperCase() + path[i].slice(1));
// stop if we've found a router
if(typeof(APP.Routers[router]) == "function") break;
}
// call the router or fallback to the default
var controller = (router && APP.Routers[router]) ? new APP.Routers[router]( options ) : new APP.Routers.Default( options );
// return controller so it's accessible through the app global
return controller;
}
};
// Namespace definition
APP.Models = {};
APP.Routers = {};
APP.Collections = {};
APP.Views = {};
APP.Layouts = {};
APP.Templates = {};
})(_, Backbone);
/**
* @name backbone.easing
* A View that has an interface for easing.js tweens
*
* Version: 0.2.3 (Wed, 02 Dec 2015 05:22:46 GMT)
* Source: http://github.com/makesites/backbone-easing
*
* @author makesites
* Initiated by: Makis Tracend (@tracend)
* Distributed through [Makesites.org](http://makesites.org)
*
* @cc_on Copyright © Makesites.org
* @license Released under the [MIT license](http://makesites.org/licenses/MIT)
*/
(function (lib) {
//"use strict";
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
var deps = ['jquery', 'underscore', 'backbone']; // condition when backbone.app is part of the array?
define('backbone.easing', deps, lib);
} else if ( typeof module === "object" && module && typeof module.exports === "object" ){
// Expose as module.exports in loaders that implement CommonJS module pattern.
module.exports = lib;
} else {
// Browser globals
var Query = $ || jQuery || Zepto || vQuery;
lib(Query, _, Backbone);
}
}(function ($, _, Backbone) {
// support for Backbone APP() view if available...
var APP = APP || window.APP || false;
var isAPP = ( APP !== false );
var View = ( isAPP && typeof APP.View !== "undefined" ) ? APP.View : Backbone.View;
// Helpers
_.mixin({
now: function(){
return ( new Date() ).getTime();
}
});
var Easing = View.extend({
el : 'body',
options : {
targetEl: window, // the element that will be animated
ease: "easeFrom",
duration: 2 // in seconds
},
// convert to model, maybe...
_transitionData: {
start: 0,
end: 0,
easing: false,
startPos: 0,
endPos: 0,
pos: 0
},
initialize: function( options ) {
_.bindAll( this, "transitionStart", "transition", "transitionEnd" );
this.options = _.extend({}, this.options, options );
this.tick = new Tick();
// get the target element
if( !this.targetEl ) {
this.targetEl = (typeof this.options.targetEl == "string" )? $( this.options.targetEl )[0] : this.options.targetEl;
}
return View.prototype.initialize.call( this, options );
},
transitionData: function( target ){
// variables
var data, start, end;
// record data
var now = _.now();
data = {
start: now,
end: now + (this.options.duration * 1000),
easing: easing[this.options.ease],
startPos: start,
endPos: end,
pos: start
};
//save for later
this._transitionData = data;
return data;
},
transitionPos: function( pos ){
if( !pos ){
// get
} else {
//set
}
},
transitionStart: function( target ){
this.transitionData( target );
// add transitio in the queue
this.tick.add( this.transition );
},
transition: function(){
var now = _.now();
var data = this._transitionData;
// condition #1: if reached our destination...
if( now > data.end ) return this.transitionEnd();
// condition #2: if the position has changed by another "force"
if( data.pos && data.pos !== this.transitionPos() ) return this.transitionEnd();
var start = data.startPos;
var end = data.endPos;
var easingFunc = data.easing;
var time = ( data.end - now ) / (this.options.duration * 1000);
var pos = Math.round( start + ( end - start ) * (1 -easingFunc( time ) ) );
this.transitionPos( pos );
// save pos for later
data.pos = pos;
this._transitionData = data;
},
transitionEnd: function() {
// remove transition
this.tick.remove( this.transition );
// can you stop tick.js?
},
// expose easing methods
tween: function( key ){
return easing[key];
}
});
// MODIFIED!
// --------------------------------------------------
// easing.js v0.5.4
// Generic set of easing functions with AMD support
// https://github.com/danro/easing-js
// This code may be freely distributed under the MIT license
// http://danro.mit-license.org/
// --------------------------------------------------
// All functions adapted from Thomas Fuchs & Jeremy Kahn
// Easing Equations (c) 2003 Robert Penner, BSD license
// https://raw.github.com/danro/easing-js/master/LICENSE
// --------------------------------------------------
(function (name, definition) {
/*global define module*/
if (typeof define == 'function') define(name, definition);
else if (typeof module != 'undefined') module.exports = definition();
// always expose methods locally
this[name] = definition();
}('easing', function(){
return {
easeInQuad: function(pos) {
return Math.pow(pos, 2);
},
easeOutQuad: function(pos) {
return -(Math.pow((pos-1), 2) -1);
},
easeInOutQuad: function(pos) {
if ((pos/=0.5) < 1) return 0.5*Math.pow(pos,2);
return -0.5 * ((pos-=2)*pos - 2);
},
easeInCubic: function(pos) {
return Math.pow(pos, 3);
},
easeOutCubic: function(pos) {
return (Math.pow((pos-1), 3) +1);
},
easeInOutCubic: function(pos) {
if ((pos/=0.5) < 1) return 0.5*Math.pow(pos,3);
return 0.5 * (Math.pow((pos-2),3) + 2);
},
easeInQuart: function(pos) {
return Math.pow(pos, 4);
},
easeOutQuart: function(pos) {
return -(Math.pow((pos-1), 4) -1);
},
easeInOutQuart: function(pos) {
if ((pos/=0.5) < 1) return 0.5*Math.pow(pos,4);
return -0.5 * ((pos-=2)*Math.pow(pos,3) - 2);
},
easeInQuint: function(pos) {
return Math.pow(pos, 5);
},
easeOutQuint: function(pos) {
return (Math.pow((pos-1), 5) +1);
},
easeInOutQuint: function(pos) {
if ((pos/=0.5) < 1) return 0.5*Math.pow(pos,5);
return 0.5 * (Math.pow((pos-2),5) + 2);
},
easeInSine: function(pos) {
return -Math.cos(pos * (Math.PI/2)) + 1;
},
easeOutSine: function(pos) {
return Math.sin(pos * (Math.PI/2));
},
easeInOutSine: function(pos) {
return (-0.5 * (Math.cos(Math.PI*pos) -1));
},
easeInExpo: function(pos) {
return (pos===0) ? 0 : Math.pow(2, 10 * (pos - 1));
},
easeOutExpo: function(pos) {
return (pos===1) ? 1 : -Math.pow(2, -10 * pos) + 1;
},
easeInOutExpo: function(pos) {
if(pos===0) return 0;
if(pos===1) return 1;
if((pos/=0.5) < 1) return 0.5 * Math.pow(2,10 * (pos-1));
return 0.5 * (-Math.pow(2, -10 * --pos) + 2);
},
easeInCirc: function(pos) {
return -(Math.sqrt(1 - (pos*pos)) - 1);
},
easeOutCirc: function(pos) {
return Math.sqrt(1 - Math.pow((pos-1), 2));
},
easeInOutCirc: function(pos) {
if((pos/=0.5) < 1) return -0.5 * (Math.sqrt(1 - pos*pos) - 1);
return 0.5 * (Math.sqrt(1 - (pos-=2)*pos) + 1);
},
easeOutBounce: function(pos) {
if ((pos) < (1/2.75)) {
return (7.5625*pos*pos);
} else if (pos < (2/2.75)) {
return (7.5625*(pos-=(1.5/2.75))*pos + 0.75);
} else if (pos < (2.5/2.75)) {
return (7.5625*(pos-=(2.25/2.75))*pos + 0.9375);
} else {
return (7.5625*(pos-=(2.625/2.75))*pos + 0.984375);
}
},
easeInBack: function(pos) {
var s = 1.70158;
return (pos)*pos*((s+1)*pos - s);
},
easeOutBack: function(pos) {
var s = 1.70158;
return (pos=pos-1)*pos*((s+1)*pos + s) + 1;
},
easeInOutBack: function(pos) {
var s = 1.70158;
if((pos/=0.5) < 1) return 0.5*(pos*pos*(((s*=(1.525))+1)*pos -s));
return 0.5*((pos-=2)*pos*(((s*=(1.525))+1)*pos +s) +2);
},
elastic: function(pos) {
return -1 * Math.pow(4,-8*pos) * Math.sin((pos*6-1)*(2*Math.PI)/2) + 1;
},
swingFromTo: function(pos) {
var s = 1.70158;
return ((pos/=0.5) < 1) ? 0.5*(pos*pos*(((s*=(1.525))+1)*pos - s)) :
0.5*((pos-=2)*pos*(((s*=(1.525))+1)*pos + s) + 2);
},
swingFrom: function(pos) {
var s = 1.70158;
return pos*pos*((s+1)*pos - s);
},
swingTo: function(pos) {
var s = 1.70158;
return (pos-=1)*pos*((s+1)*pos + s) + 1;
},
bounce: function(pos) {
if (pos < (1/2.75)) {
return (7.5625*pos*pos);
} else if (pos < (2/2.75)) {
return (7.5625*(pos-=(1.5/2.75))*pos + 0.75);
} else if (pos < (2.5/2.75)) {
return (7.5625*(pos-=(2.25/2.75))*pos + 0.9375);
} else {
return (7.5625*(pos-=(2.625/2.75))*pos + 0.984375);
}
},
bouncePast: function(pos) {
if (pos < (1/2.75)) {
return (7.5625*pos*pos);
} else if (pos < (2/2.75)) {
return 2 - (7.5625*(pos-=(1.5/2.75))*pos + 0.75);
} else if (pos < (2.5/2.75)) {
return 2 - (7.5625*(pos-=(2.25/2.75))*pos + 0.9375);
} else {
return 2 - (7.5625*(pos-=(2.625/2.75))*pos + 0.984375);
}
},
easeFromTo: function(pos) {
if ((pos/=0.5) < 1) return 0.5*Math.pow(pos,4);
return -0.5 * ((pos-=2)*Math.pow(pos,3) - 2);
},
easeFrom: function(pos) {
return Math.pow(pos,4);
},
easeTo: function(pos) {
return Math.pow(pos,0.25);
}
};
}));
/* Tick.js
* Source: https://github.com/makesites/tick
* Copyright © Makesites.org
*
* Initiated by Makis Tracend (@tracend)
* Distributed through [Makesites.org](http://makesites.org)
* Released under the [MIT license](http://makesites.org/licenses/MIT)
*/
(function( window ){
var Tick = function( options ){
// merge options
options = options || {};
if(options.rate) this.options.rate = options.rate;
// setup animation rate
this.rate();
// start loop
this.loop();
};
Tick.prototype = {
options: {
rate: 1000 / 60 // standard 60fps
},
queue: [],
rate: function( rate ){
rate = rate || this.options.rate;
// RequestAnimationFrame shim - Source: http://paulirish.com/2011/requestanimationframe-for-smart-animating/
window.requestAnimFrame = window.requestAnimFrame || ( function( callback ) {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function ( callback ) {
window.setTimeout( function () {
callback(+new Date());
}, rate );
};
})();
},
loop: function( timestamp ){
// feature-detect if rAF and now() are of the same scale (epoch or high-res),
// if not, we have to do a timestamp fix on each frame
//Source: https://github.com/ded/morpheus/blob/master/src/morpheus.js#L91
//if (fixTs) timestamp = now();
this.process( timestamp );
// reset loop
window.requestAnimFrame(this.loop.bind(this)); // bind only works in > ES5
},
process: function( timestamp ){
// loop through queue
for( var i in this.queue ){
var item = this.queue[i];
// prerequisite
if( typeof item.fn !== "function") continue;
// restrict execution if not time yet
var step = (timestamp % item.interval);
if( step === 0 || item.run + item.interval > timestamp) continue;
// run
item.fn(); // context?
// record last run
// condition in case the item was released in the meantime...
if( this.queue[i] ) this.queue[i].run = timestamp;
}
},
// interface
add: function( fn, interval ){
// prerequisite
if( typeof fn !== "function" ) return;
// fallback
interval = interval || this.options.rate;
var item = {
fn: fn,
interval: interval,
run: 0
};
this.queue.push( item );
},
remove: function( fn ){
var exists = false;
for( var i in this.queue ){
var item = this.queue[i];
if( String(item.fn) === String(fn) ){
exists = true;
delete this.queue[i];
}
}
return exists;
}
};
// save to the global namespace
window.Tick = Tick;
})( this.window );
// update Backbone namespace regardless
Backbone.Easing = Easing;
if( isAPP ){
APP.Easing = Easing;
}
// If there is a window object, that at least has a document property
if ( typeof window === "object" && typeof window.document === "object" ) {
window.Backbone = Backbone;
// update APP namespace
if( isAPP ){
window.APP = APP;
}
}
// for module loaders:
return Easing;
}));
(function(window, Backbone) {
'use strict';
var loadUrl = Backbone.History.prototype.loadUrl;
Backbone.History.prototype.loadUrl = function(fragmentOverride) {
var matched = loadUrl.apply(this, arguments),
gaFragment = this.fragment;
if (!/^\//.test(gaFragment)) {
gaFragment = '/' + gaFragment;
}
// legacy version
if (typeof window._gaq !== "undefined") {
window._gaq.push(['_trackPageview', gaFragment]);
}
// Analytics.js
var ga;
if (window.GoogleAnalyticsObject && window.GoogleAnalyticsObject !== 'ga') {
ga = window.GoogleAnalyticsObject;
} else {
ga = window.ga;
}
if (typeof ga !== 'undefined') {
ga('send', 'pageview', gaFragment);
}
return matched;
};
})(window, Backbone);
// Backbone Extender
// Extending objects like events and options when using extend() in main constructors
//
// Source: https://gist.github.com/tracend/5425415
(function(_, Backbone){
var origExtend = Backbone.Model.extend;
var extend = function(protoProps, staticProps) {
var parent = this;
var child;
if (protoProps){
_.each(protoProps, function(value, key){
// exit now if the types can't be extended
if( typeof value == "string" || typeof value == "boolean" ) return;
// modify only the objects that are available in the parent
if( key in parent.prototype && !(value instanceof Function) && !(parent.prototype[key] instanceof Function) && !(value instanceof Backbone.Model) && !(parent.prototype[key] instanceof Backbone.Model) && !(value instanceof Backbone.Collection) && !(parent.prototype[key] instanceof Backbone.Collection) ){
protoProps[key] = _.extend({}, parent.prototype[key], value);
}
});
}
// FIX: can't use original .extend to contain child prototype
//return origExtend.call(this, protoProps, staticProps);
// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your `extend` definition), or defaulted
// by us to simply call the parent's constructor.
if (protoProps && _.has(protoProps, 'constructor')) {
child = protoProps.constructor;
} else {
child = function(){ return parent.apply(this, arguments); };
}
// Add static properties to the constructor function, if supplied.
_.extend(child, parent, staticProps);
// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
var Surrogate = function(){ this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = Object.create(new Surrogate());
// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) child.prototype = _.extend({}, child.prototype, protoProps);
// Set a convenience property in case the parent's prototype is needed
// later.
child.__super__ = parent.prototype;
return child;
};
// Set up inheritance for the model, collection, router, view and history.
Backbone.Model.extend = Backbone.Collection.extend = Backbone.Router.extend = Backbone.View.extend = extend;
// Plus add a whildcard _extend_ that behaves like Array's concat
Backbone.extend = function(){
var classes = Array.prototype.slice.call(arguments, 0);
// prerequisites
if( !classes.length ) return;
var Class = classes.shift(); // pick first element
// loop through classes
for( var i in classes ){
var Child = classes[i];
//var Parent = Class.extend({}); // clone...
var Parent = Class;
var proto = ( Child.prototype ) ? Child.prototype : Child;
// only object accepted (lookup instance of Backbone...?)
if(typeof proto !== "object" ) continue;
// clone methods
//var methods = Object.create( proto ); // why not working??
var methods = _.extend({}, proto);
// FIX delete old parent reference
delete methods._parent;
// save reference to parent class
methods._parent = Parent; // add this only if not the final loop
// extend parent
Class = Parent.extend( methods );
}
return Class;
};
})(_, Backbone);
var utils = {
// Common.js extend method: https://github.com/commons/common.js
extend: function(){
var objects = Array.prototype.slice.call( arguments ); // to array?
var destination = {};
for( var obj in objects ){
var source = objects[obj];
for (var property in source){
if (source[property] && source[property].constructor && source[property].constructor === Object) {
destination[property] = destination[property] || {};
destination[property] = arguments.callee(destination[property], source[property]);
} else {
destination[property] = source[property];
}
}
}
return destination;
}
};
// Underscore
(function(_, Backbone, $) {
// Helpers
// this is to enable {{moustache}} syntax to simple _.template() calls
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g,
variable : "."
};
// if available, use the Handlebars compiler
if(typeof Handlebars != "undefined"){
_.mixin({
template : Handlebars.compile
});
}
})(_, Backbone, jQuery);
/*
* Backbone.ready()
* Source: https://gist.github.com/tracend/5617079
*
* by Makis Tracend( @tracend )
*
* Usage:
* Backbone.ready( callback );
*
*/
(function(window, document, $, Backbone){
// find the $
$ = $ || ( ('$' in window) ? window.$ : window.jQuery || window.Zepto || false ); // need to account for "local" $
Backbone.ready = function( callback ){
if( isPhonegap() ){
return PhoneGap.init( callback );
} else if( $ ) {
// use the 'default' ready event
return $(document).ready( callback );
} else if (window.addEventListener) {
// ultimate fallback, add window event - trigger the page as soon it's loaded
return window.addEventListener('load', callback, false);
} else {
// IE...
return window.attachEvent('onload', callback);
}
};
// Helpers
// - Support Phonegap Shim: https://github.com/makesites/phonegap-shim
function isPhonegap(){
// only execute in app mode?
return typeof PhoneGap != "undefined" && typeof PhoneGap.init != "undefined" && typeof PhoneGap.env != "undefined" && PhoneGap.env.app;
}
return Backbone;
})(window, document, $, Backbone);
/*
* Backbone States
* Source: https://github.com/makesites/backbone-states
*
* Created by Makis Tracend ( [@tracend](http://github.com/tracend) )
* Released under the [MIT license](http://makesites.org/licenses/MIT)
*
*/
(function(window, document, Backbone){
// find the $
//$ = ('$' in window) ? window.$ : window.jQuery || window.Zepto || false;
var View = Backbone.View;
Backbone.View = View.extend({
states: {
},
initialize: function(options){
this.initStates();
return View.prototype.initialize.call(this, options);
},
initStates: function(){
for(var e in this.states){
var method = this.states[e];
this.bind(e, _.bind(this[method], this) );
}
}
});
// Helpers
//...
return Backbone;
})(window, document, Backbone);
(function(_, Backbone, APP) {
// **Main constructors**
APP.Model = Backbone.Model.extend({
options: {
autofetch: false,
cache: false
},
// initialization
initialize: function( model, options ){
// save options for later
options = options || {};
this.options = _.extend({}, this.options, options);
// set data if given
if( !_.isNull( model ) && !_.isEmpty( model ) ) this.set( model );
// restore cache
if( this.options.cache ){
var cache = this.cache();
if( cache ) this.set( cache );
}
// auto-fetch if no models are passed
if( this.options.autofetch && !_.isUndefined(this.url) ){
this.fetch();
}
},
// add is like set but only if not available
add: function( obj ){
var self = this;
var data = {};
_.each( obj, function( item, key ){
if( _.isUndefined( self.get(key) ) ){
data[key] = item;
}
});
this.set( data );
},
// #63 reset model to its default values
reset: function(){
return this.clear().set(this.defaults);
},
// Use Backbone.cache, if available
cache: Backbone.Model.prototype.cache || function(){
// optionally create your own custom a cache mechanism...
return false;
},
// Helper functions
// - check if the app is online
isOnline: function(){
return ( !_.isUndefined( app ) ) ? app.state.online : true;
},
// FIX: override sync to support DELETE method (411 error on NGINX)
// issue: http://serverfault.com/q/396020
/*
sync : function(method, model, options) {
var methodMap = { 'create': 'POST', 'update': 'PUT', 'delete': 'DELETE', 'read': 'GET' };
var type = methodMap[method];
var opt = options || (options = {});
var params = {type: type, dataType: 'json', data: {}};
if (!options.url) {
params.url = this.getValue(model, 'url') || urlError();
}
if (!options.data && model && (method == 'create' || method == 'update')) {
params.contentType = 'application/json';
params.data = JSON.stringify(model.toJSON());
}
if (params.type !== 'GET' && !Backbone.emulateJSON) {
params.processData = false;
}
return $.ajax(_.extend(params, options));
},
*/
// Helper - DELETE if the sync is not needed any more...
getValue : function(object, prop) {
if (!(object && object[prop])) return null;
return _.isFunction(object[prop]) ? object[prop]() : object[prop];
},
parse: function( data ){
var self = this;
setTimeout(function(){ self.trigger("fetch"); }, 200); // better way to trigger this after parse?
// cache response
if( this.options.cache ){
this.cache( data );
}
return data;
},
// extract data (and possibly filter keys)
output: function(){
// in most cases it's a straight JSON output
return this.toJSON();
}
});
// *** Extensions ***
MongoModel = APP.Model.extend({
parse: function( data ){
//console.log(data);
// "normalize" result with proper ids
if(data._id){
data.id = data._id;
delete data._id;
}
return data;
}
});
})(_, Backbone, APP);
(function(_, Backbone, APP) {
APP.Collection = Backbone.Collection.extend({