-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.marionette.genie.js
755 lines (646 loc) · 20 KB
/
backbone.marionette.genie.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
// GenieJS (Backbone.Marionette.Genie)
// -----------------------------------
// v0.3.6
//
// Copyright (c) 2013 Michael Spencer; Trynd, LLC
// Distributed under the MIT license
//
// http://github.com/trynd/geniejs
(function(Backbone, Marionette, _) {
"use strict";
var Controller, Duct, Genie, Region, Router, Vent, helperConstructor,
listenLocal, listenRemote, moduleFunctions, parseOptions, previousGenie,
previousMarionetteGenie, root;
// Existing environment status
root = this;
previousGenie = root.Genie;
previousMarionetteGenie = Marionette.Genie;
// Genie Core
// ----------
// Genie constructor
Genie = function(options) {
this.options = _.extend({}, options);
this.parseOptions(this.options, this.validOptions);
this.processOptions(this.options);
this.define = this.createDefine(this.options);
if (_.isFunction(this.initialize)) {
this.initialize(options);
}
return this;
};
// Valid Genie options
Genie.prototype.validOptions = [
'startWithParent',
'initializer',
'vent',
'duct',
'region',
'regions',
'controller',
'router',
'passStartOptionsToVent',
'passStartOptionsToDuct',
'passStartOptionsToRegion',
'passStartOptionsToController',
'passStartOptionsToRouter',
'ventOptions',
'ductOptions',
'regionOptions',
'controllerOptions',
'routerOptions'
];
// Combine passed in options and extended fields
// Modified the "options" object
Genie.prototype.parseOptions = parseOptions = function(options, validOptions)
{
if (_.isObject(options)) {
var i, len, option;
for (i = 0, len = validOptions.length; i < len; i++) {
option = validOptions[i];
if (!(option in options)) {
if (option in this) {
options[option] = this[option];
}
}
}
} else {
options = {};
}
return options;
};
// Handle any module callback object fields
// Modifies the "options" object
Genie.prototype.processOptions = function(options) {
if ('startWithParent' in options) {
this.startWithParent = options.startWithParent;
} else {
this.startWithParent = true;
}
return options;
};
// Prepare options for creating a helper class by merging various option sets
Genie.prototype.mergeHelperClassOptions = function(requiredOptions,
customOptions, passStartOptions, startOptions)
{
var options = {};
if (passStartOptions === true) {
_.extend(options, startOptions);
}
_.extend(options, requiredOptions);
if (_.isObject(customOptions)) {
_.extend(options, customOptions);
}
return options;
};
// Create the module callback object define() based on Genie options
Genie.prototype.createDefine = function() {
var genie = this;
return function() {
// Retain a reference to the Genie object
this.genie = genie;
// Retain a self reference for consistency
this.mod = this;
// Incorporate Genie module functions
_.extend(this, moduleFunctions);
this.listenLocal = listenLocal;
this.listenRemote = listenRemote;
// Add the module's initializer
return this.addInitializer(function(startOptions) {
var genieOptions;
genieOptions = genie.options;
// store initialization options
this.startOptions = startOptions;
// Create Vent
if ('vent' in genieOptions) {
this.addVent(genieOptions.vent);
}
// Create Duct
if ('duct' in genieOptions) {
this.addDuct(genieOptions.duct);
}
// Create Regions
if ('region' in genieOptions) {
this.addRegion('region', genieOptions.region);
}
if ('regions' in genieOptions) {
this.addRegions(genieOptions.regions);
}
// Create Controller
if ('controller' in genieOptions) {
this.addController(genieOptions.controller);
}
// Create Router
if ('router' in genieOptions) {
this.addRouter(genieOptions.router);
}
// Run custom initializer
if ('initializer' in genieOptions) {
genieOptions.initializer.apply(this, arguments);
}
return this;
});
};
};
// Genie module functions
moduleFunctions = {
// Set a Controller on the module
addController: function(definition, options) {
if (definition != null && definition !== false) {
var controller;
// A definition of true instantiates a new Controller object
if (definition === true) {
definition = Controller;
}
// A function definition instantiates a new function instance
if (_.isFunction(definition)) {
options = this.genie.mergeHelperClassOptions(
{
app: this.app,
mod: this
},
_.isObject(options)
? options
: this.genie.options.controllerOptions,
this.genie.options.passStartOptionsToController,
this.startOptions
);
controller = new definition(options);
} else {
controller = definition;
}
return this.controller = controller;
}
return;
},
// Set a Duct on the module
addDuct: function(definition, options) {
if (definition != null && definition !== false) {
var duct;
// A definition of true instantiates a new Duct object
if (definition === true) {
definition = Duct;
}
// A function definition instantiates a new function instance
if (_.isFunction(definition)) {
options = this.genie.mergeHelperClassOptions(
{
app: this.app,
mod: this
},
_.isObject(options) ? options : this.genie.options.ductOptions,
this.genie.options.passStartOptionsToDuct,
this.startOptions
);
duct = new definition(options);
} else {
duct = definition;
}
return this.duct = duct;
}
return;
},
// Add a Region to the module
addRegion: function(name, definition, options) {
if (!_.isString(name)) {
options = definition;
definition = name;
name = 'region';
}
if (name != null && definition != null) {
var isFunction, isString, region;
// If the definition is an array, assume a definition and options
if (_.isArray(definition)) {
if (!_.isObject(options)) {
options = definition[1];
}
definition = definition[0];
}
// Do nothing if defined as false
if (definition !== false) {
isString = _.isString(definition);
isFunction = !isString && _.isFunction(definition);
if (!('regions' in this)) {
this.regions = {};
}
if (isString || isFunction) {
options = this.genie.mergeHelperClassOptions(
{
app: this.app,
mod: this
},
_.isObject(options) ? options : this.genie.options.regionOptions,
this.genie.options.passStartOptionsToRegion,
this.startOptions
);
if (isString) {
options.el = definition;
region = new Region(options);
} else {
region = new definition(options);
}
} else {
region = definition;
}
this.regions[name] = region;
if (name === 'region') {
this.region = region;
}
return region;
}
}
return;
},
// Add multiple Regions to the module
addRegions: function(definitions) {
if (_.isObject(definitions)) {
_.each(definitions, function(region, name) {
this.addRegion(name, region);
}, this);
}
return;
},
// Set a Router on the module
addRouter: function(definition, options) {
if (definition != null && definition !== false) {
var router;
// A definition of true instantiates a new Router object
if (definition === true) {
definition = Router;
}
// A function definition instantiates a new function instance
if (_.isFunction(definition)) {
options = this.genie.mergeHelperClassOptions(
{
app: this.app,
mod: this
},
_.isObject(options) ? options : this.genie.options.routerOptions,
this.genie.options.passStartOptionsToRouter,
this.startOptions
);
// If the module already has a Controller and one has not been
// specified in the options, pass the module's controller to the new
// Router
if (_.isObject(this.controller) && !('controller' in options)) {
options.controller = this.controller;
}
router = new definition(options);
} else {
router = definition;
}
return this.router = router;
}
return;
},
// Set a Vent on the module
addVent: function(definition, options) {
if (definition != null && definition !== false) {
var vent;
// A definition of true instantiates a new Vent object
if (definition === true) {
definition = Vent;
}
// A function definition instantiates a new function instance
if (_.isFunction(definition)) {
options = this.genie.mergeHelperClassOptions(
{
app: this.app,
mod: this
},
_.isObject(options) ? options : this.genie.options.ventOptions,
this.genie.options.passStartOptionsToVent,
this.startOptions
);
vent = new definition(options);
} else {
vent = definition;
}
return this.vent = vent;
}
return;
}
};
// Used to add a module initializer to the Genie object after instantiation
Genie.prototype.addInitializer = function(initializer) {
if (_.isFunction(initializer)) {
if (!('initializer' in this.options)) {
this.options.initializer = initializer;
} else {
var oldInitializer = this.options.initializer;
this.options.initializer = function(){
oldInitializer.apply(this, arguments);
initializer.apply(this, arguments);
};
}
}
};
// Used to add "controller" and "controllerOptions" options to the Genie
// object after instantiation
Genie.prototype.addController = function(definition, options) {
if (definition != null) {
this.options.controller = definition;
if (_.isObject(options)) {
this.options.controllerOptions = _.extend({}, options);
}
}
return;
}
// Used to add "duct" and "ductOptions" options to the Genie object after
// instantiation
Genie.prototype.addDuct = function(definition, options) {
if (definition != null) {
this.options.duct = definition;
if (_.isObject(options)) {
this.options.ductOptions = _.extend({}, options);
}
}
return;
}
// Used to add "region" or "regions" options to the Genie object after
// instantiation
Genie.prototype.addRegion = function(name, definition, options) {
if (!_.isString(name)) {
options = definition;
definition = name;
name = 'region';
}
var region = definition;
if (_.isObject(options)) {
if (_.isArray(region)) {
region = [region[0], _.extend({}, options)];
} else {
region = [region, _.extend({}, options)];
}
}
if (name === 'region') {
this.options.region = region;
} else {
if (!('regions' in this.options)) {
this.options.regions = {};
}
this.options.regions[name] = region;
}
return;
}
// Used to add "region" or "regions" options to the Genie object after
// instantiation
Genie.prototype.addRegions = function(definitions) {
if (_.isObject(definitions)) {
_.each(definitions, function(region, name) {
this.addRegion(name, region);
}, this);
}
return;
}
// Used to add "router" and "routerOptions" options to the Genie object after
// instantiation
Genie.prototype.addRouter = function(definition, options) {
if (definition != null) {
this.options.router = definition;
if (_.isObject(options)) {
this.options.routerOptions = _.extend({}, options);
}
}
return;
}
// Used to add "vent" and "ventOptions" options to the Genie object after
// instantiation
Genie.prototype.addVent = function(definition, options) {
if (definition != null) {
this.options.vent = definition;
if (_.isObject(options)) {
this.options.ventOptions = _.extend({}, options);
}
}
return;
}
// Inherit standard Backbone object extend() functionality
Genie.extend = Backbone.Model.extend;
// Set the environment's "Genie" back to its previous value and return Genie
Genie.noConflict = function() {
root.Genie = previousGenie;
return this;
};
// Set Marionette.Genie back to its default status and return Genie
Genie.noConflictMarionette = function() {
Marionette.Genie = previousMarionetteGenie;
return this;
};
// Helper Objects
// --------------
// Helper class constructor
// Inherit standard option fields
helperConstructor = function(options) {
if (_.isObject(options)) {
this.options = _.extend({}, options);
if ('app' in options) {
this.app = options.app;
}
if ('mod' in options) {
this.mod = options.mod;
}
}
return this;
};
// Vent
// ----
// An event aggregator with initialize() functionality
// Vent constructor
Genie.Vent = Vent = function(options) {
helperConstructor.apply(this, arguments);
if (_.isFunction(this.initialize)) {
this.initialize(options);
}
return this;
};
// Inherit extend() functionality
Vent.extend = Genie.extend;
// Inherit Backbone.Events functionality
_.extend(Vent.prototype, Backbone.Events);
// Duct
// ----
// Connects two Vent objects, mapping events between them
// Duct constructor
Genie.Duct = Duct = function(options) {
helperConstructor.apply(this, arguments);
this.options = this.parseOptions(this.options, this.validOptions);
if (!('local' in this.options)) {
if (_.isObject(this.mod)) {
this.options.local = this.mod.vent;
}
}
if (!('remote' in this.options)) {
if (_.isObject(this.app)) {
this.options.remote = this.app.vent;
}
}
this.local = this.options.local;
this.remote = this.options.remote;
if (_.isFunction(this.initialize)) {
this.initialize(options);
}
return this;
};
// Valid Duct options
Duct.prototype.validOptions = [
'local',
'remote'
];
// Inherit Genie's standard option parsing
Duct.prototype.parseOptions = parseOptions;
// Map messages from one Vent object to another
Duct.prototype.map = function(source, destination, sourceMessage,
destinationMessage, mapper, mapperContext)
{
if (sourceMessage != null) {
if (destinationMessage == null) {
destinationMessage = sourceMessage;
}
return this.listenTo(source, sourceMessage, function() {
var args;
args = arguments;
if (_.isFunction(mapper)) {
if (mapperContext == null) {
mapperContext = this;
}
args = mapper.apply(mapperContext, args);
}
if (args === true) {
args = arguments;
} else if (args === false) {
return;
}
if (!(_.isArguments(args) || _.isArray(args))) {
return;
}
return destination.trigger.apply(destination,
[destinationMessage].concat(Array.prototype.slice.call(args)));
});
}
return;
};
// Map a message from the remote Vent object to the local one
Duct.prototype.in = function(remoteMessage, localMessage, mapper,
mapperContext)
{
return this.map(this.remote, this.local, remoteMessage, localMessage,
mapper, mapperContext);
};
// Map a message from the local Vent object to the remote one
Duct.prototype.out = function(localMessage, remoteMessage, mapper,
mapperContext)
{
return this.map(this.local, this.remote, localMessage, remoteMessage,
mapper, mapperContext);
};
// Map a message from the defined "app" Application object to the local vent
Duct.prototype.fromApp = function(appMessage, localMessage, mapper,
mapperContext)
{
return this.map(this.app, this.local, appMessage, localMessage,
mapper, mapperContext);
};
// Map a message from the defined "mod" Module object to the local vent
Duct.prototype.fromMod = function(modMessage, localMessage, mapper,
mapperContext)
{
return this.map(this.mod, this.local, modMessage, localMessage,
mapper, mapperContext);
};
// Map one local message to another local message
Duct.prototype.fromLocal = function(firstMessage, secondMessage, mapper,
mapperContext)
{
return this.map(this.local, this.local, firstMessage, secondMessage,
mapper, mapperContext);
};
// Map a message on the local vent to a callback
Duct.prototype.listenLocal = function(message, callback, context){
if (!!this.local) {
return this.listenTo(this.local, message, callback, context);
}
throw 'local vent not defined';
};
// Map a message on the remote vent to a callback
Duct.prototype.listenRemote = function(message, callback, context){
if (!!this.remote) {
return this.listenTo(this.remote, message, callback, context);
}
throw 'remote vent not defined';
};
// Inherit extend() functionality
Duct.extend = Genie.extend;
// Inherit Backbone.Events functionality
_.extend(Duct.prototype, Backbone.Events);
// Region
// ------
// Region constructor
Genie.Region = Region = Marionette.Region.extend({
constructor: function() {
helperConstructor.apply(this, arguments);
Marionette.Region.apply(this, arguments);
return this;
}
});
// Controller
// ----------
// Controller constructor
Genie.Controller = Controller = Marionette.Controller.extend({
constructor: function() {
helperConstructor.apply(this, arguments);
Marionette.Controller.apply(this, arguments);
return this;
}
});
// Router
// ------
// Router constructor
Genie.Router = Router = Marionette.AppRouter.extend({
constructor: function() {
helperConstructor.apply(this, arguments);
Marionette.AppRouter.apply(this, arguments);
return this;
}
});
// Helper Methods
// --------------
// listenLocal()
listenLocal = function(message, callback, context){
var target;
if (this.mod != null) {
if (this.mod.duct != null) {
if (this.mod.duct.local != null) {
target = this.mod.duct.local;
}
} else if (this.mod.vent != null) {
target = this.mod.vent;
}
}
if (!!target) {
return this.listenTo(target, message, callback, context);
}
throw 'local vent not defined';
};
// listenRemote()
listenRemote = function(message, callback, context){
var target;
if (this.mod != null && this.mod.duct != null) {
if (this.mod.duct.remote != null) {
target = this.mod.duct.remote;
}
} else if (this.app != null && this.app.vent != null) {
target = this.mod.vent;
}
if (!!target) {
return this.listenTo(target, message, callback, context);
}
throw 'remote vent not defined';
};
// Map listen* methods
_.each([Vent, Region, Controller, Router], function(helper){
helper.prototype.listenLocal = listenLocal;
helper.prototype.listenRemote = listenRemote;
});
// Export to applicable namespaces
return root.Genie = Marionette.Genie = Genie;
}).call(this, Backbone, Backbone.Marionette, _);