updated backbone, marionette, sugar, underscore, modelbinder, bootstrap.switch

This commit is contained in:
Keivan Beigi 2013-04-18 16:14:08 -07:00
parent 99cd327002
commit 549b497770
16 changed files with 4336 additions and 3747 deletions

View file

@ -1,23 +1,23 @@
// Backbone.CollectionBinder v0.1.1
// (c) 2012 Bart Wood
// Backbone.CollectionBinder v1.0.0
// (c) 2013 Bart Wood
// Distributed Under MIT License
(function () {
(function(){
if (!Backbone) {
if(!Backbone){
throw 'Please include Backbone.js before Backbone.ModelBinder.js';
}
if (!Backbone.ModelBinder) {
if(!Backbone.ModelBinder){
throw 'Please include Backbone.ModelBinder.js before Backbone.CollectionBinder.js';
}
Backbone.CollectionBinder = function (elManagerFactory, options) {
Backbone.CollectionBinder = function(elManagerFactory, options){
_.bindAll(this);
this._elManagers = {};
this._elManagerFactory = elManagerFactory;
if (!this._elManagerFactory) throw 'elManagerFactory must be defined.';
if(!this._elManagerFactory) throw 'elManagerFactory must be defined.';
// Let the factory just use the trigger function on the view binder
this._elManagerFactory.trigger = this.trigger;
@ -25,14 +25,14 @@
this._options = options || {};
};
Backbone.CollectionBinder.VERSION = '0.1.1';
Backbone.CollectionBinder.VERSION = '1.0.0';
_.extend(Backbone.CollectionBinder.prototype, Backbone.Events, {
bind: function (collection, parentEl) {
bind: function(collection, parentEl){
this.unbind();
if (!collection) throw 'collection must be defined';
if (!parentEl) throw 'parentEl must be defined';
if(!collection) throw 'collection must be defined';
if(!parentEl) throw 'parentEl must be defined';
this._collection = collection;
this._elManagerFactory.setParentEl(parentEl);
@ -45,8 +45,8 @@
},
unbind: function () {
if (this._collection !== undefined) {
unbind: function(){
if(this._collection !== undefined){
this._collection.off('add', this._onCollectionAdd);
this._collection.off('remove', this._onCollectionRemove);
this._collection.off('reset', this._onCollectionReset);
@ -55,13 +55,13 @@
this._removeAllElManagers();
},
getManagerForEl: function (el) {
getManagerForEl: function(el){
var i, elManager, elManagers = _.values(this._elManagers);
for (i = 0; i < elManagers.length; i++) {
for(i = 0; i < elManagers.length; i++){
elManager = elManagers[i];
if (elManager.isElContained(el)) {
if(elManager.isElContained(el)){
return elManager;
}
}
@ -69,13 +69,13 @@
return undefined;
},
getManagerForModel: function (model) {
getManagerForModel: function(model){
var i, elManager, elManagers = _.values(this._elManagers);
for (i = 0; i < elManagers.length; i++) {
for(i = 0; i < elManagers.length; i++){
elManager = elManagers[i];
if (elManager.getModel() === model) {
if(elManager.getModel() === model){
return elManager;
}
}
@ -83,31 +83,31 @@
return undefined;
},
_onCollectionAdd: function (model) {
_onCollectionAdd: function(model){
this._elManagers[model.cid] = this._elManagerFactory.makeElManager(model);
this._elManagers[model.cid].createEl();
if (this._options['autoSort']) {
if(this._options['autoSort']){
this.sortRootEls();
}
},
_onCollectionRemove: function (model) {
_onCollectionRemove: function(model){
this._removeElManager(model);
},
_onCollectionReset: function () {
_onCollectionReset: function(){
this._removeAllElManagers();
this._collection.each(function (model) {
this._collection.each(function(model){
this._onCollectionAdd(model);
}, this);
this.trigger('elsReset', this._collection);
},
_removeAllElManagers: function () {
_.each(this._elManagers, function (elManager) {
_removeAllElManagers: function(){
_.each(this._elManagers, function(elManager){
elManager.removeEl();
delete this._elManagers[elManager._model.cid];
}, this);
@ -116,21 +116,21 @@
this._elManagers = {};
},
_removeElManager: function (model) {
if (this._elManagers[model.cid] !== undefined) {
_removeElManager: function(model){
if(this._elManagers[model.cid] !== undefined){
this._elManagers[model.cid].removeEl();
delete this._elManagers[model.cid];
}
},
sortRootEls: function () {
this._collection.each(function (model, modelIndex) {
sortRootEls: function(){
this._collection.each(function(model, modelIndex){
var modelElManager = this.getManagerForModel(model);
if (modelElManager) {
if(modelElManager){
var modelEl = modelElManager.getEl();
var currentRootEls = this._elManagerFactory.getParentEl().children();
var currentRootEls = $(this._elManagerFactory.getParentEl()).children();
if (currentRootEls[modelIndex] !== modelEl[0]) {
if(currentRootEls[modelIndex] !== modelEl[0]){
modelEl.detach();
modelEl.insertBefore(currentRootEls[modelIndex]);
}
@ -142,40 +142,40 @@
// The ElManagerFactory is used for els that are just html templates
// elHtml - how the model's html will be rendered. Must have a single root element (div,span).
// bindings (optional) - either a string which is the binding attribute (name, id, data-name, etc.) or a normal bindings hash
Backbone.CollectionBinder.ElManagerFactory = function (elHtml, bindings) {
Backbone.CollectionBinder.ElManagerFactory = function(elHtml, bindings){
_.bindAll(this);
this._elHtml = elHtml;
this._bindings = bindings;
if (!_.isString(this._elHtml)) throw 'elHtml must be a valid html string';
if(! _.isString(this._elHtml)) throw 'elHtml must be a valid html string';
};
_.extend(Backbone.CollectionBinder.ElManagerFactory.prototype, {
setParentEl: function (parentEl) {
setParentEl: function(parentEl){
this._parentEl = parentEl;
},
getParentEl: function () {
getParentEl: function(){
return this._parentEl;
},
makeElManager: function (model) {
makeElManager: function(model){
var elManager = {
_model: model,
createEl: function () {
createEl: function(){
this._el = $(this._elHtml);
this._el = $(this._elHtml);
$(this._parentEl).append(this._el);
if (this._bindings) {
if (_.isString(this._bindings)) {
if(this._bindings){
if(_.isString(this._bindings)){
this._modelBinder = new Backbone.ModelBinder();
this._modelBinder.bind(this._model, this._el, Backbone.ModelBinder.createDefaultBindings(this._el, this._bindings));
}
else if (_.isObject(this._bindings)) {
else if(_.isObject(this._bindings)){
this._modelBinder = new Backbone.ModelBinder();
this._modelBinder.bind(this._model, this._el, this._bindings);
}
@ -187,8 +187,8 @@
this.trigger('elCreated', this._model, this._el);
},
removeEl: function () {
if (this._modelBinder !== undefined) {
removeEl: function(){
if(this._modelBinder !== undefined){
this._modelBinder.unbind();
}
@ -196,15 +196,15 @@
this.trigger('elRemoved', this._model, this._el);
},
isElContained: function (findEl) {
isElContained: function(findEl){
return this._el === findEl || $(this._el).has(findEl).length > 0;
},
getModel: function () {
getModel: function(){
return this._model;
},
getEl: function () {
getEl: function(){
return this._el;
}
};
@ -218,36 +218,36 @@
// The ViewManagerFactory is used for els that are created and owned by backbone views.
// There is no bindings option because the view made by the viewCreator should take care of any binding
// viewCreator - a callback that will create backbone view instances for a model passed to the callback
Backbone.CollectionBinder.ViewManagerFactory = function (viewCreator) {
Backbone.CollectionBinder.ViewManagerFactory = function(viewCreator){
_.bindAll(this);
this._viewCreator = viewCreator;
if (!_.isFunction(this._viewCreator)) throw 'viewCreator must be a valid function that accepts a model and returns a backbone view';
if(!_.isFunction(this._viewCreator)) throw 'viewCreator must be a valid function that accepts a model and returns a backbone view';
};
_.extend(Backbone.CollectionBinder.ViewManagerFactory.prototype, {
setParentEl: function (parentEl) {
setParentEl: function(parentEl){
this._parentEl = parentEl;
},
getParentEl: function () {
getParentEl: function(){
return this._parentEl;
},
makeElManager: function (model) {
makeElManager: function(model){
var elManager = {
_model: model,
createEl: function () {
createEl: function(){
this._view = this._viewCreator(model);
$(this._parentEl).append(this._view.render(this._model).el);
this.trigger('elCreated', this._model, this._view);
},
removeEl: function () {
if (this._view.close !== undefined) {
removeEl: function(){
if(this._view.close !== undefined){
this._view.close();
}
else {
@ -258,19 +258,19 @@
this.trigger('elRemoved', this._model, this._view);
},
isElContained: function (findEl) {
isElContained: function(findEl){
return this._view.el === findEl || this._view.$el.has(findEl).length > 0;
},
getModel: function () {
getModel: function(){
return this._model;
},
getView: function () {
getView: function(){
return this._view;
},
getEl: function () {
getEl: function(){
return this._view.$el;
}
};
@ -281,4 +281,4 @@
}
});
}).call(this);
}).call(this);