|
|
@ -1,6 +1,6 @@
|
|
|
|
// MarionetteJS (Backbone.Marionette)
|
|
|
|
// MarionetteJS (Backbone.Marionette)
|
|
|
|
// ----------------------------------
|
|
|
|
// ----------------------------------
|
|
|
|
// v1.0.3
|
|
|
|
// v1.0.4
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Copyright (c)2013 Derick Bailey, Muted Solutions, LLC.
|
|
|
|
// Copyright (c)2013 Derick Bailey, Muted Solutions, LLC.
|
|
|
|
// Distributed under MIT license
|
|
|
|
// Distributed under MIT license
|
|
|
@ -19,7 +19,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
// Backbone.BabySitter
|
|
|
|
// Backbone.BabySitter
|
|
|
|
// -------------------
|
|
|
|
// -------------------
|
|
|
|
// v0.0.5
|
|
|
|
// v0.0.6
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Copyright (c)2013 Derick Bailey, Muted Solutions, LLC.
|
|
|
|
// Copyright (c)2013 Derick Bailey, Muted Solutions, LLC.
|
|
|
|
// Distributed under MIT license
|
|
|
|
// Distributed under MIT license
|
|
|
@ -37,14 +37,13 @@ Backbone.ChildViewContainer = (function(Backbone, _){
|
|
|
|
// Container Constructor
|
|
|
|
// Container Constructor
|
|
|
|
// ---------------------
|
|
|
|
// ---------------------
|
|
|
|
|
|
|
|
|
|
|
|
var Container = function(initialViews){
|
|
|
|
var Container = function(views){
|
|
|
|
this._views = {};
|
|
|
|
this._views = {};
|
|
|
|
this._indexByModel = {};
|
|
|
|
this._indexByModel = {};
|
|
|
|
this._indexByCollection = {};
|
|
|
|
|
|
|
|
this._indexByCustom = {};
|
|
|
|
this._indexByCustom = {};
|
|
|
|
this._updateLength();
|
|
|
|
this._updateLength();
|
|
|
|
|
|
|
|
|
|
|
|
this._addInitialViews(initialViews);
|
|
|
|
_.each(views, this.add, this);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Container Methods
|
|
|
|
// Container Methods
|
|
|
@ -54,7 +53,7 @@ Backbone.ChildViewContainer = (function(Backbone, _){
|
|
|
|
|
|
|
|
|
|
|
|
// Add a view to this container. Stores the view
|
|
|
|
// Add a view to this container. Stores the view
|
|
|
|
// by `cid` and makes it searchable by the model
|
|
|
|
// by `cid` and makes it searchable by the model
|
|
|
|
// and/or collection of the view. Optionally specify
|
|
|
|
// cid (and model itself). Optionally specify
|
|
|
|
// a custom key to store an retrieve the view.
|
|
|
|
// a custom key to store an retrieve the view.
|
|
|
|
add: function(view, customIndex){
|
|
|
|
add: function(view, customIndex){
|
|
|
|
var viewCid = view.cid;
|
|
|
|
var viewCid = view.cid;
|
|
|
@ -67,11 +66,6 @@ Backbone.ChildViewContainer = (function(Backbone, _){
|
|
|
|
this._indexByModel[view.model.cid] = viewCid;
|
|
|
|
this._indexByModel[view.model.cid] = viewCid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// index it by collection
|
|
|
|
|
|
|
|
if (view.collection){
|
|
|
|
|
|
|
|
this._indexByCollection[view.collection.cid] = viewCid;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// index by custom
|
|
|
|
// index by custom
|
|
|
|
if (customIndex){
|
|
|
|
if (customIndex){
|
|
|
|
this._indexByCustom[customIndex] = viewCid;
|
|
|
|
this._indexByCustom[customIndex] = viewCid;
|
|
|
@ -81,18 +75,16 @@ Backbone.ChildViewContainer = (function(Backbone, _){
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// Find a view by the model that was attached to
|
|
|
|
// Find a view by the model that was attached to
|
|
|
|
// it. Uses the model's `cid` to find it, and
|
|
|
|
// it. Uses the model's `cid` to find it.
|
|
|
|
// retrieves the view by it's `cid` from the result
|
|
|
|
|
|
|
|
findByModel: function(model){
|
|
|
|
findByModel: function(model){
|
|
|
|
var viewCid = this._indexByModel[model.cid];
|
|
|
|
return this.findByModelCid(model.cid);
|
|
|
|
return this.findByCid(viewCid);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// Find a view by the collection that was attached to
|
|
|
|
// Find a view by the `cid` of the model that was attached to
|
|
|
|
// it. Uses the collection's `cid` to find it, and
|
|
|
|
// it. Uses the model's `cid` to find the view `cid` and
|
|
|
|
// retrieves the view by it's `cid` from the result
|
|
|
|
// retrieve the view using it.
|
|
|
|
findByCollection: function(col){
|
|
|
|
findByModelCid: function(modelCid){
|
|
|
|
var viewCid = this._indexByCollection[col.cid];
|
|
|
|
var viewCid = this._indexByModel[modelCid];
|
|
|
|
return this.findByCid(viewCid);
|
|
|
|
return this.findByCid(viewCid);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
@ -122,26 +114,13 @@ Backbone.ChildViewContainer = (function(Backbone, _){
|
|
|
|
delete this._indexByModel[view.model.cid];
|
|
|
|
delete this._indexByModel[view.model.cid];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// delete collection index
|
|
|
|
|
|
|
|
if (view.collection){
|
|
|
|
|
|
|
|
delete this._indexByCollection[view.collection.cid];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// delete custom index
|
|
|
|
// delete custom index
|
|
|
|
var cust;
|
|
|
|
_.any(this._indexByCustom, function(cid, key) {
|
|
|
|
|
|
|
|
if (cid === viewCid) {
|
|
|
|
for (var key in this._indexByCustom){
|
|
|
|
delete this._indexByCustom[key];
|
|
|
|
if (this._indexByCustom.hasOwnProperty(key)){
|
|
|
|
return true;
|
|
|
|
if (this._indexByCustom[key] === viewCid){
|
|
|
|
|
|
|
|
cust = key;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
|
|
|
|
if (cust){
|
|
|
|
|
|
|
|
delete this._indexByCustom[cust];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// remove the view from the container
|
|
|
|
// remove the view from the container
|
|
|
|
delete this._views[viewCid];
|
|
|
|
delete this._views[viewCid];
|
|
|
@ -153,44 +132,24 @@ Backbone.ChildViewContainer = (function(Backbone, _){
|
|
|
|
// Call a method on every view in the container,
|
|
|
|
// Call a method on every view in the container,
|
|
|
|
// passing parameters to the call method one at a
|
|
|
|
// passing parameters to the call method one at a
|
|
|
|
// time, like `function.call`.
|
|
|
|
// time, like `function.call`.
|
|
|
|
call: function(method, args){
|
|
|
|
call: function(method){
|
|
|
|
args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
this.apply(method, _.tail(arguments));
|
|
|
|
this.apply(method, args);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// Apply a method on every view in the container,
|
|
|
|
// Apply a method on every view in the container,
|
|
|
|
// passing parameters to the call method one at a
|
|
|
|
// passing parameters to the call method one at a
|
|
|
|
// time, like `function.apply`.
|
|
|
|
// time, like `function.apply`.
|
|
|
|
apply: function(method, args){
|
|
|
|
apply: function(method, args){
|
|
|
|
var view;
|
|
|
|
_.each(this._views, function(view){
|
|
|
|
|
|
|
|
|
|
|
|
// fix for IE < 9
|
|
|
|
|
|
|
|
args = args || [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_.each(this._views, function(view, key){
|
|
|
|
|
|
|
|
if (_.isFunction(view[method])){
|
|
|
|
if (_.isFunction(view[method])){
|
|
|
|
view[method].apply(view, args);
|
|
|
|
view[method].apply(view, args || []);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// Update the `.length` attribute on this container
|
|
|
|
// Update the `.length` attribute on this container
|
|
|
|
_updateLength: function(){
|
|
|
|
_updateLength: function(){
|
|
|
|
this.length = _.size(this._views);
|
|
|
|
this.length = _.size(this._views);
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// set up an initial list of views
|
|
|
|
|
|
|
|
_addInitialViews: function(views){
|
|
|
|
|
|
|
|
if (!views){ return; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var view, i,
|
|
|
|
|
|
|
|
length = views.length;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i=0; i<length; i++){
|
|
|
|
|
|
|
|
view = views[i];
|
|
|
|
|
|
|
|
this.add(view);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
@ -1418,7 +1377,13 @@ Marionette.View = Backbone.View.extend({
|
|
|
|
// A single item view implementation that contains code for rendering
|
|
|
|
// A single item view implementation that contains code for rendering
|
|
|
|
// with underscore.js templates, serializing the view's model or collection,
|
|
|
|
// with underscore.js templates, serializing the view's model or collection,
|
|
|
|
// and calling several methods on extended views, such as `onRender`.
|
|
|
|
// and calling several methods on extended views, such as `onRender`.
|
|
|
|
Marionette.ItemView = Marionette.View.extend({
|
|
|
|
Marionette.ItemView = Marionette.View.extend({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Setting up the inheritance chain which allows changes to
|
|
|
|
|
|
|
|
// Marionette.View.prototype.constructor which allows overriding
|
|
|
|
|
|
|
|
constructor: function(){
|
|
|
|
|
|
|
|
Marionette.View.prototype.constructor.apply(this, slice(arguments));
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// Serialize the model or collection for the view. If a model is
|
|
|
|
// Serialize the model or collection for the view. If a model is
|
|
|
|
// found, `.toJSON()` is called. If a collection is found, `.toJSON()`
|
|
|
|
// found, `.toJSON()` is called. If a collection is found, `.toJSON()`
|
|
|
@ -1750,6 +1715,12 @@ Marionette.CollectionView = Marionette.View.extend({
|
|
|
|
// an item view as `modelView`, for the top leaf
|
|
|
|
// an item view as `modelView`, for the top leaf
|
|
|
|
Marionette.CompositeView = Marionette.CollectionView.extend({
|
|
|
|
Marionette.CompositeView = Marionette.CollectionView.extend({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Setting up the inheritance chain which allows changes to
|
|
|
|
|
|
|
|
// Marionette.CollectionView.prototype.constructor which allows overriding
|
|
|
|
|
|
|
|
constructor: function(){
|
|
|
|
|
|
|
|
Marionette.CollectionView.prototype.constructor.apply(this, slice(arguments));
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// Configured the initial events that the composite view
|
|
|
|
// Configured the initial events that the composite view
|
|
|
|
// binds to. Override this method to prevent the initial
|
|
|
|
// binds to. Override this method to prevent the initial
|
|
|
|
// events, or to add your own initial events.
|
|
|
|
// events, or to add your own initial events.
|
|
|
@ -1893,7 +1864,7 @@ Marionette.Layout = Marionette.ItemView.extend({
|
|
|
|
this._firstRender = true;
|
|
|
|
this._firstRender = true;
|
|
|
|
this._initializeRegions(options);
|
|
|
|
this._initializeRegions(options);
|
|
|
|
|
|
|
|
|
|
|
|
Marionette.ItemView.call(this, options);
|
|
|
|
Marionette.ItemView.prototype.constructor.call(this, options);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// Layout's render will use the existing region objects the
|
|
|
|
// Layout's render will use the existing region objects the
|
|
|
|