marionette-rails 0.9.12 → 0.9.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
|
|
1
|
-
// Backbone.Marionette, v0.9.
|
1
|
+
// Backbone.Marionette, v0.9.13
|
2
2
|
// Copyright (c)2012 Derick Bailey, Muted Solutions, LLC.
|
3
3
|
// Distributed under MIT license
|
4
4
|
// http://github.com/derickbailey/backbone.marionette
|
@@ -66,19 +66,23 @@ Marionette.EventBinder.extend = Backbone.View.extend;
|
|
66
66
|
|
67
67
|
// The core view type that other Marionette views extend from.
|
68
68
|
Marionette.View = Backbone.View.extend({
|
69
|
+
|
69
70
|
constructor: function(){
|
70
71
|
var eventBinder = new Marionette.EventBinder();
|
71
72
|
_.extend(this, eventBinder);
|
72
73
|
|
73
74
|
Backbone.View.prototype.constructor.apply(this, arguments);
|
74
75
|
|
76
|
+
this.bindBackboneEntityTo(this.model, this.modelEvents);
|
77
|
+
this.bindBackboneEntityTo(this.collection, this.collectionEvents);
|
78
|
+
|
75
79
|
this.bindTo(this, "show", this.onShowCalled, this);
|
76
80
|
},
|
77
81
|
|
78
82
|
// Get the template for this view
|
79
83
|
// instance. You can set a `template` attribute in the view
|
80
84
|
// definition or pass a `template: "whatever"` parameter in
|
81
|
-
// to the constructor options.
|
85
|
+
// to the constructor options.
|
82
86
|
getTemplate: function(){
|
83
87
|
var template;
|
84
88
|
|
@@ -96,14 +100,14 @@ Marionette.View = Backbone.View.extend({
|
|
96
100
|
// Serialize the model or collection for the view. If a model is
|
97
101
|
// found, `.toJSON()` is called. If a collection is found, `.toJSON()`
|
98
102
|
// is also called, but is used to populate an `items` array in the
|
99
|
-
// resulting data. If both are found, defaults to the model.
|
100
|
-
// You can override the `serializeData` method in your own view
|
103
|
+
// resulting data. If both are found, defaults to the model.
|
104
|
+
// You can override the `serializeData` method in your own view
|
101
105
|
// definition, to provide custom serialization for your view's data.
|
102
106
|
serializeData: function(){
|
103
107
|
var data;
|
104
108
|
|
105
|
-
if (this.model) {
|
106
|
-
data = this.model.toJSON();
|
109
|
+
if (this.model) {
|
110
|
+
data = this.model.toJSON();
|
107
111
|
}
|
108
112
|
else if (this.collection) {
|
109
113
|
data = { items: this.collection.toJSON() };
|
@@ -205,10 +209,24 @@ Marionette.View = Backbone.View.extend({
|
|
205
209
|
var selector = that.uiBindings[key];
|
206
210
|
that.ui[key] = that.$(selector);
|
207
211
|
});
|
208
|
-
}
|
212
|
+
},
|
209
213
|
|
210
|
-
|
214
|
+
// This method is used to bind a backbone "entity" (collection/model) to methods on the view.
|
215
|
+
bindBackboneEntityTo: function(entity, bindings){
|
216
|
+
if (!entity || !bindings) { return; }
|
217
|
+
|
218
|
+
var view = this;
|
219
|
+
_.each(bindings, function(methodName, evt){
|
220
|
+
|
221
|
+
var method = view[methodName];
|
222
|
+
if(!method) {
|
223
|
+
throw new Error("View method '"+ methodName +"' was configured as an event handler, but does not exist.");
|
224
|
+
}
|
211
225
|
|
226
|
+
view.bindTo(entity, evt, method, view);
|
227
|
+
});
|
228
|
+
}
|
229
|
+
});
|
212
230
|
|
213
231
|
// Item View
|
214
232
|
// ---------
|
@@ -269,7 +287,7 @@ Marionette.CollectionView = Marionette.View.extend({
|
|
269
287
|
this.onShowCallbacks = new Marionette.Callbacks();
|
270
288
|
},
|
271
289
|
|
272
|
-
// Configured the initial events that the collection view
|
290
|
+
// Configured the initial events that the collection view
|
273
291
|
// binds to. Override this method to prevent the initial
|
274
292
|
// events, or to add your own initial events.
|
275
293
|
initialEvents: function(){
|
@@ -283,7 +301,7 @@ Marionette.CollectionView = Marionette.View.extend({
|
|
283
301
|
// Handle a child item added to the collection
|
284
302
|
addChildView: function(item, collection, options){
|
285
303
|
this.closeEmptyView();
|
286
|
-
var ItemView = this.getItemView();
|
304
|
+
var ItemView = this.getItemView(item);
|
287
305
|
return this.addItemView(item, ItemView, options.index);
|
288
306
|
},
|
289
307
|
|
@@ -331,8 +349,9 @@ Marionette.CollectionView = Marionette.View.extend({
|
|
331
349
|
// collection view and show it
|
332
350
|
showCollection: function(){
|
333
351
|
var that = this;
|
334
|
-
var ItemView
|
352
|
+
var ItemView;
|
335
353
|
this.collection.each(function(item, index){
|
354
|
+
ItemView = that.getItemView(item);
|
336
355
|
that.addItemView(item, ItemView, index);
|
337
356
|
});
|
338
357
|
},
|
@@ -362,7 +381,7 @@ Marionette.CollectionView = Marionette.View.extend({
|
|
362
381
|
// Retrieve the itemView type, either from `this.options.itemView`
|
363
382
|
// or from the `itemView` in the object definition. The "options"
|
364
383
|
// takes precedence.
|
365
|
-
getItemView: function(){
|
384
|
+
getItemView: function(item){
|
366
385
|
var itemView = this.options.itemView || this.itemView;
|
367
386
|
|
368
387
|
if (!itemView){
|
@@ -381,7 +400,7 @@ Marionette.CollectionView = Marionette.View.extend({
|
|
381
400
|
|
382
401
|
var view = this.buildItemView(item, ItemView);
|
383
402
|
|
384
|
-
// Store the child view itself so we can properly
|
403
|
+
// Store the child view itself so we can properly
|
385
404
|
// remove and/or close it later
|
386
405
|
this.storeChild(view);
|
387
406
|
if (this.onItemAdded){ this.onItemAdded(view); }
|
@@ -409,17 +428,17 @@ Marionette.CollectionView = Marionette.View.extend({
|
|
409
428
|
// them when removing / closing the child view
|
410
429
|
this.childBindings = this.childBindings || {};
|
411
430
|
this.childBindings[view.cid] = childBinding;
|
412
|
-
|
431
|
+
|
413
432
|
return renderResult;
|
414
433
|
},
|
415
|
-
|
434
|
+
|
416
435
|
// render the item view
|
417
436
|
renderItemView: function(view, index) {
|
418
437
|
view.render();
|
419
438
|
this.appendHtml(this, view, index);
|
420
439
|
},
|
421
440
|
|
422
|
-
// Build an `itemView` for every model in the collection.
|
441
|
+
// Build an `itemView` for every model in the collection.
|
423
442
|
buildItemView: function(item, ItemView){
|
424
443
|
var itemViewOptions = _.result(this, "itemViewOptions");
|
425
444
|
var options = _.extend({model: item}, itemViewOptions);
|
@@ -471,8 +490,8 @@ Marionette.CollectionView = Marionette.View.extend({
|
|
471
490
|
close: function(){
|
472
491
|
this.trigger("collection:before:close");
|
473
492
|
this.closeChildren();
|
474
|
-
Marionette.View.prototype.close.apply(this, arguments);
|
475
493
|
this.trigger("collection:closed");
|
494
|
+
Marionette.View.prototype.close.apply(this, arguments);
|
476
495
|
},
|
477
496
|
|
478
497
|
// Close the child views that this collection view
|
@@ -500,7 +519,7 @@ Marionette.CompositeView = Marionette.CollectionView.extend({
|
|
500
519
|
this.itemView = this.getItemView();
|
501
520
|
},
|
502
521
|
|
503
|
-
// Configured the initial events that the composite view
|
522
|
+
// Configured the initial events that the composite view
|
504
523
|
// binds to. Override this method to prevent the initial
|
505
524
|
// events, or to add your own initial events.
|
506
525
|
initialEvents: function(){
|
@@ -515,8 +534,16 @@ Marionette.CompositeView = Marionette.CollectionView.extend({
|
|
515
534
|
// the items in the collection. The default is to return
|
516
535
|
// `this.itemView` or Marionette.CompositeView if no `itemView`
|
517
536
|
// has been defined
|
518
|
-
getItemView: function(){
|
519
|
-
|
537
|
+
getItemView: function(item){
|
538
|
+
var itemView = this.options.itemView || this.itemView || this.constructor;
|
539
|
+
|
540
|
+
if (!itemView){
|
541
|
+
var err = new Error("An `itemView` must be specified");
|
542
|
+
err.name = "NoItemViewError";
|
543
|
+
throw err;
|
544
|
+
}
|
545
|
+
|
546
|
+
return itemView;
|
520
547
|
},
|
521
548
|
|
522
549
|
// Renders the model once, and the collection once. Calling
|
@@ -708,8 +735,8 @@ Marionette.Layout = Marionette.ItemView.extend({
|
|
708
735
|
regionType: Marionette.Region,
|
709
736
|
|
710
737
|
constructor: function () {
|
711
|
-
Backbone.Marionette.ItemView.apply(this, arguments);
|
712
738
|
this.initializeRegions();
|
739
|
+
Backbone.Marionette.ItemView.apply(this, arguments);
|
713
740
|
},
|
714
741
|
|
715
742
|
// Layout's render will use the existing region objects the
|