forked from marionettejs/backbone.marionette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.marionette.js
728 lines (599 loc) · 21.3 KB
/
backbone.marionette.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
// Backbone.Marionette v0.6.3
//
// Copyright (C)2011 Derick Bailey, Muted Solutions, LLC
// Distributed Under MIT License
//
// Documentation and Full License Available at:
// http://github.com/derickbailey/backbone.marionette
Backbone.Marionette = (function(Backbone, _, $){
var Marionette = {};
Marionette.version = "0.6.3";
// Item View
// ---------
// A single item view implementation that contains code for rendering
// with underscore.js templates, serializing the view's model or collection,
// and calling several methods on extended views, such as `onRender`.
Marionette.ItemView = Backbone.View.extend({
constructor: function(){
var args = slice.call(arguments);
Backbone.View.prototype.constructor.apply(this, args);
_.bindAll(this, "render");
this.initialEvents();
},
// Configured the initial events that the item view
// binds to. Override this method to prevent the initial
// events, or to add your own initial events.
initialEvents: function(){
if (this.collection){
this.bindTo(this.collection, "reset", this.render, this);
}
},
// Serialize the model or collection for the view. If a model is
// found, `.toJSON()` is called. If a collection is found, `.toJSON()`
// is also called, but is used to populate an `items` array in the
// resulting data. If both are found, defaults to the model.
// You can override the `serializeData` method in your own view
// definition, to provide custom serialization for your view's data.
serializeData: function(){
var data;
if (this.model) { data = this.model.toJSON(); }
else if (this.collection) {
data = { items: this.collection.toJSON() };
}
return data;
},
// Render the view, defaulting to underscore.js templates.
// You can override this in your view definition.
render: function(){
var that = this;
var data = this.serializeData();
var deferredRender = $.Deferred();
this.beforeRender && this.beforeRender();
this.trigger("item:before:render", that);
deferredRender.done(function(){
that.onRender && that.onRender();
that.trigger("item:rendered", that);
});
var templateRetrieval = this.getTemplate();
$.when(templateRetrieval).then(function(template){
var html = that.renderTemplate(template, data);
that.$el.html(html);
deferredRender.resolve();
});
return deferredRender.promise();
},
// Default implementation uses underscore.js templates. Override
// this method to use your own templating engine.
renderTemplate: function(template, data){
if (!template || template.length === 0){
var msg = "A template must be specified";
var err = new Error(msg);
err.name = "NoTemplateError";
throw err;
}
return _.template(template, data);
},
// Retrieve the template from the call's context. The
// `template` attribute can either be a function that
// returns a jQuery object, or a jQuery selector string
// directly. The string value must be a valid jQuery
// selector.
getTemplate: function(){
var template = this.template || this.options.template;
if (_.isFunction(template)){
template = template.call(this);
}
return Marionette.TemplateCache.get(template);
},
// Default `close` implementation, for removing a view from the
// DOM and unbinding it. Regions will call this method
// for you. You can specify an `onClose` method in your view to
// add custom code that is called after the view is closed.
close: function(){
this.trigger('item:before:close');
this.beforeClose && this.beforeClose();
this.unbindAll();
this.unbind();
this.remove();
this.onClose && this.onClose();
this.trigger('item:closed');
}
});
// Collection View
// ---------------
// A view that iterates over a Backbone.Collection
// and renders an individual ItemView for each model.
Marionette.CollectionView = Backbone.View.extend({
constructor: function(){
Backbone.View.prototype.constructor.apply(this, arguments);
_.bindAll(this, "addChildView", "render");
this.initialEvents();
},
// Configured the initial events that the collection view
// binds to. Override this method to prevent the initial
// events, or to add your own initial events.
initialEvents: function(){
this.bindTo(this.collection, "add", this.addChildView, this);
this.bindTo(this.collection, "remove", this.removeChildView, this);
this.bindTo(this.collection, "reset", this.render, this);
},
// Loop through all of the items and render
// each of them with the specified `itemView`.
render: function(){
var that = this;
var deferredRender = $.Deferred();
var promises = [];
this.beforeRender && this.beforeRender();
this.trigger("collection:before:render", this);
this.closeChildren();
if (!this.itemView){
var err = new Error("An `itemView` must be specified");
err.name = "NoItemViewError";
throw err;
}
this.collection.each(function(item){
var promise = that.addChildView(item)
promises.push(promise);
});
deferredRender.done(function(){
this.onRender && this.onRender();
this.trigger("collection:rendered", this);
});
$.when(promises).then(function(){
deferredRender.resolveWith(that);
});
return deferredRender.promise();
},
// Render the child item's view and add it to the
// HTML for the collection view.
addChildView: function(item){
var that = this;
var view = new this.itemView({
model: item
});
this.storeChild(view);
var promise = view.render();
$.when(promise).then(function(){
that.appendHtml(that.$el, view.$el);
});
return promise;
},
// Remove the child view and close it
removeChildView: function(item){
var view = this.children[item.cid];
if (view){
view.close();
delete this.children[item.cid];
}
},
// Append the HTML to the collection's `el`.
// Override this method to do something other
// then `.append`.
appendHtml: function(el, html){
el.append(html);
},
// Store references to all of the child `itemView`
// instances so they can be managed and cleaned up, later.
storeChild: function(view){
if (!this.children){
this.children = {};
}
this.children[view.model.cid] = view;
},
// Handle cleanup and other closing needs for
// the collection of views.
close: function(){
this.beforeClose && this.beforeClose();
this.trigger("collection:before:close");
this.unbind();
this.unbindAll();
this.closeChildren();
this.remove();
this.onClose && this.onClose();
this.trigger("collection:closed");
},
closeChildren: function(){
if (this.children){
_.each(this.children, function(childView){
childView.close();
});
}
}
});
// Composite View
// --------------
// Used for rendering a branch-leaf, hierarchical structure.
// Extends directly from CollectionView and also renders an
// an item view as `modelView`, for the top leaf
Marionette.CompositeView = Marionette.CollectionView.extend({
modelView: Marionette.ItemView,
// Renders the model once, and the collection once. Calling
// this again will tell the model's view to re-render itself
// but the collection will not re-render.
render: function(){
var that = this;
var compositeRendered = $.Deferred();
this.renderedModelView = new this.modelView({
model: this.model,
template: this.template
});
var modelIsRendered = this.renderModel();
$.when(modelIsRendered).then(function(){
that.$el.html(that.renderedModelView.el);
that.trigger("composite:model:rendered");
var collectionIsRendered = that.renderCollection();
$.when(collectionIsRendered).then(function(){
compositeRendered.resolve();
});
});
compositeRendered.done(function(){
that.trigger("composite:rendered");
});
return compositeRendered.promise();
},
// Render the collection for the composite view
renderCollection: function(){
var collectionDeferred = Marionette.CollectionView.prototype.render.apply(this, arguments);
collectionDeferred.done(function(){
this.trigger("composite:collection:rendered");
});
return collectionDeferred.promise();
},
// Render an individual model, if we have one, as
// part of a composite view (branch / leaf). For example:
// a treeview.
renderModel: function(){
if (this.renderedModelView){
return this.renderedModelView.render();
}
},
// Ensure we close things correctly
close: function(){
Marionette.CollectionView.prototype.close.apply(this, arguments);
if (this.renderedModelView){
this.renderedModelView.close && this.renderedModelView.close();
delete this.renderedModelView;
}
}
});
// Region
// ------
// Manage the visual regions of your composite application. See
// http://lostechies.com/derickbailey/2011/12/12/composite-js-apps-regions-and-region-managers/
Marionette.Region = function(options){
this.options = options || {};
_.extend(this, options);
if (!this.el){
var err = new Error("An 'el' must be specified");
err.name = "NoElError";
throw err;
}
};
_.extend(Marionette.Region.prototype, Backbone.Events, {
// Displays a backbone view instance inside of the region.
// Handles calling the `render` method for you. Reads content
// directly from the `el` attribute. Also calls an optional
// `onShow` and `close` method on your view, just after showing
// or just before closing the view, respectively.
show: function(view, appendMethod){
this.ensureEl();
this.close();
this.open(view, appendMethod);
this.currentView = view;
},
ensureEl: function(){
if (!this.$el || this.$el.length == 0){
this.$el = this.getEl(this.el);
}
},
// Override this method to change how the region finds the
// DOM element that it manages. Return a jQuery selector object.
getEl: function(selector){
return $(selector);
},
// Internal method to render and display a view. Not meant
// to be called from any external code.
open: function(view, appendMethod){
var that = this;
appendMethod = appendMethod || "html";
$.when(view.render()).then(function () {
that.$el[appendMethod](view.el);
view.onShow && view.onShow();
that.trigger("view:show", view);
});
},
// Close the current view, if there is one. If there is no
// current view, it does nothing and returns immediately.
close: function(){
var view = this.currentView;
if (!view){ return; }
view.close && view.close();
this.trigger("view:closed", view);
delete this.currentView;
},
// Attach an existing view to the region. This
// will not call `render` or `onShow` for the new view,
// and will not replace the current HTML for the `el`
// of the region.
attachView: function(view){
this.currentView = view;
}
});
// Layout
// ------
// Formerly known as Composite Region.
//
// Used for managing application layouts, nested layouts and
// multiple regions within an application or sub-application.
//
// A specialized view type that renders an area of HTML and then
// attaches `Region` instances to the specified `regions`.
// Used for composite view management and sub-application areas.
Marionette.Layout = Marionette.ItemView.extend({
constructor: function () {
this.vent = new Backbone.Marionette.EventAggregator();
Backbone.Marionette.ItemView.apply(this, arguments);
this.regionManagers = {};
},
render: function () {
this.initializeRegions();
return Backbone.Marionette.ItemView.prototype.render.call(this, arguments);
},
close: function () {
this.closeRegions();
Backbone.Marionette.ItemView.prototype.close.call(this, arguments);
},
initializeRegions: function () {
var that = this;
_.each(this.regions, function (selector, name) {
var regionManager = new Backbone.Marionette.Region({
el: selector,
getEl: function(selector){
return that.$(selector);
}
});
that.regionManagers[name] = regionManager;
that[name] = regionManager;
});
},
closeRegions: function () {
var that = this;
_.each(this.regionManagers, function (manager, name) {
manager.close();
delete that[name];
});
this.regionManagers = {};
}
});
// AppRouter
// ---------
// Reduce the boilerplate code of handling route events
// and then calling a single method on another object.
// Have your routers configured to call the method on
// your object, directly.
//
// Configure an AppRouter with `appRoutes`.
//
// App routers can only take one `controller` object.
// It is reocmmended that you divide your controller
// objects in to smaller peices of related functionality
// and have multiple routers / controllers, instead of
// just one giant router and controller.
//
// You can also add standard routes to an AppRouter.
Marionette.AppRouter = Backbone.Router.extend({
constructor: function(options){
Backbone.Router.prototype.constructor.call(this, options);
if (this.appRoutes){
var controller = this.controller;
if (options && options.controller) {
controller = options.controller;
}
this.processAppRoutes(controller, this.appRoutes);
}
},
processAppRoutes: function(controller, appRoutes){
var method, methodName;
var route, routesLength;
var routes = [];
var router = this;
for(route in appRoutes){
routes.unshift([route, appRoutes[route]]);
}
routesLength = routes.length;
for (var i = 0; i < routesLength; i++){
route = routes[i][0];
methodName = routes[i][1];
method = _.bind(controller[methodName], controller);
router.route(route, methodName, method);
}
}
});
// BindTo: Event Binding
// ---------------------
// BindTo facilitates the binding and unbinding of events
// from objects that extend `Backbone.Events`. It makes
// unbinding events, even with anonymous callback functions,
// easy.
//
// Thanks to Johnny Oshika for this code.
// http://stackoverflow.com/questions/7567404/backbone-js-repopulate-or-recreate-the-view/7607853#7607853
Marionette.BindTo = {
// Store the event binding in array so it can be unbound
// easily, at a later point in time.
bindTo: function (obj, eventName, callback, context) {
context = context || this;
obj.on(eventName, callback, context);
if (!this.bindings) this.bindings = [];
this.bindings.push({
obj: obj,
eventName: eventName,
callback: callback,
context: context
});
},
// Unbind all of the events that we have stored.
unbindAll: function () {
_.each(this.bindings, function (binding) {
binding.obj.off(binding.eventName, binding.callback);
});
this.bindings = [];
}
};
// Callbacks
// ---------
// A simple way of managing a collection of callbacks
// and executing them at a later point in time, using jQuery's
// `Deferred` object.
Marionette.Callbacks = function(){
this.deferred = $.Deferred();
this.promise = this.deferred.promise();
};
_.extend(Marionette.Callbacks.prototype, {
// Add a callback to be executed. Callbacks added here are
// guaranteed to execute, even if they are added after the
// `run` method is called.
add: function(callback){
this.promise.done(function(context, options){
callback.call(context, options);
});
},
// Run all registered callbacks with the context specified.
// Additional callbacks can be added after this has been run
// and they will still be executed.
run: function(context, options){
this.deferred.resolve(context, options);
}
});
// Event Aggregator
// ----------------
// A pub-sub object that can be used to decouple various parts
// of an application through event-driven architecture.
Marionette.EventAggregator = function(options){
_.extend(this, options);
};
_.extend(Marionette.EventAggregator.prototype, Backbone.Events, Marionette.BindTo, {
// Assumes the event aggregator itself is the
// object being bound to.
bindTo: function(eventName, callback, context){
Marionette.BindTo.bindTo.call(this, this, eventName, callback, context);
}
});
// Composite Application
// ---------------------
// Contain and manage the composite application as a whole.
// Stores and starts up `Region` objects, includes an
// event aggregator as `app.vent`
Marionette.Application = function(options){
this.initCallbacks = new Marionette.Callbacks();
this.vent = new Marionette.EventAggregator();
_.extend(this, options);
};
_.extend(Marionette.Application.prototype, Backbone.Events, {
// Add an initializer that is either run at when the `start`
// method is called, or run immediately if added after `start`
// has already been called.
addInitializer: function(initializer){
this.initCallbacks.add(initializer);
},
// kick off all of the application's processes.
// initializes all of the regions that have been added
// to the app, and runs all of the initializer functions
start: function(options){
this.trigger("initialize:before", options);
this.initCallbacks.run(this, options);
this.trigger("initialize:after", options);
this.trigger("start", options);
},
// Add regions to your app.
// Accepts a hash of named strings or Region objects
// addRegions({something: "#someRegion"})
// addRegions{{something: Region.extend({el: "#someRegion"}) });
addRegions: function(regions){
var regionValue, regionObj;
for(var region in regions){
if (regions.hasOwnProperty(region)){
regionValue = regions[region];
if (typeof regionValue === "string"){
regionObj = new Marionette.Region({
el: regionValue
});
} else {
regionObj = new regionValue;
}
this[region] = regionObj;
}
}
}
});
// Template Cache
// --------------
// Manage templates stored in `<script>` blocks,
// caching them for faster access.
Marionette.TemplateCache = {
templates: {},
loaders: {},
// Get the specified template by id. Either
// retrieves the cached version, or loads it
// from the DOM.
get: function(templateId){
var that = this;
var templateRetrieval = $.Deferred();
var cachedTemplate = this.templates[templateId];
if (cachedTemplate){
templateRetrieval.resolve(cachedTemplate);
} else {
var loader = this.loaders[templateId];
if(loader) {
templateRetrieval = loader;
} else {
this.loaders[templateId] = templateRetrieval;
this.loadTemplate(templateId, function(template){
delete that.loaders[templateId];
that.templates[templateId] = template;
templateRetrieval.resolve(template);
});
}
}
return templateRetrieval.promise();
},
// Load a template from the DOM, by default. Override
// this method to provide your own template retrieval,
// such as asynchronous loading from a server.
loadTemplate: function(templateId, callback){
var template = $(templateId).html();
callback.call(this, template);
},
// Clear templates from the cache. If no arguments
// are specified, clears all templates:
// `clear()`
//
// If arguments are specified, clears each of the
// specified templates from the cache:
// `clear("#t1", "#t2", "...")`
clear: function(){
var length = arguments.length;
if (length > 0){
for(var i=0; i<length; i++){
delete this.templates[arguments[i]];
}
} else {
this.templates = {};
}
}
};
// Helpers
// -------
// For slicing `arguments` in functions
var slice = Array.prototype.slice;
// Copy the `extend` function used by Backbone's classes
var extend = Backbone.View.extend;
Marionette.Region.extend = extend;
Marionette.Application.extend = extend;
// Copy the features of `BindTo` on to these objects
_.extend(Marionette.ItemView.prototype, Marionette.BindTo);
_.extend(Marionette.CollectionView.prototype, Marionette.BindTo);
_.extend(Marionette.Application.prototype, Marionette.BindTo);
_.extend(Marionette.Region.prototype, Marionette.BindTo);
return Marionette;
})(Backbone, _, window.jQuery || window.Zepto || window.ender);