-
Notifications
You must be signed in to change notification settings - Fork 159
/
Backbone.ModelBinder.min.js
5 lines (4 loc) · 8.4 KB
/
Backbone.ModelBinder.min.js
1
2
3
4
5
// Backbone.ModelBinder v1.1.1
// (c) 2020 Bart Wood
// Distributed Under MIT License
!function(t){"function"==typeof define&&define.amd?define(["underscore","jquery","backbone"],t):"undefined"!=typeof module&&module.exports?module.exports=t(require("underscore"),require("jquery"),require("backbone")):t(_,jQuery,Backbone)}(function(s,u,d){if(!d)throw new Error("Please include Backbone.js before Backbone.ModelBinder.js");return d.ModelBinder=function(){s.bindAll.apply(s,[this].concat(s.functions(this)))},d.ModelBinder.SetOptions=function(t){d.ModelBinder.options=t},d.ModelBinder.VERSION="1.1.1",d.ModelBinder.Constants={},d.ModelBinder.Constants.ModelToView="ModelToView",d.ModelBinder.Constants.ViewToModel="ViewToModel",s.extend(d.ModelBinder.prototype,{bind:function(t,e,i,n){this.unbind(),this._model=t,this._rootEl=e,this._setOptions(n),this._model||this._throwException("model must be specified"),this._rootEl||this._throwException("rootEl must be specified"),i?(this._attributeBindings=u.extend(!0,{},i),this._initializeAttributeBindings(),this._initializeElBindings()):this._initializeDefaultBindings(),this._bindModelToView(),this._bindViewToModel()},bindCustomTriggers:function(t,e,i,n,o){this._triggers=i,this.bind(t,e,n,o)},unbind:function(){this._unbindModelToView(),this._unbindViewToModel(),this._attributeBindings&&(delete this._attributeBindings,this._attributeBindings=void 0)},_setOptions:function(t){this._options=s.extend({boundAttribute:"name"},d.ModelBinder.options,t),this._options.modelSetOptions||(this._options.modelSetOptions={}),this._options.modelSetOptions.changeSource="ModelBinder",this._options.changeTriggers||(this._options.changeTriggers={"":"change","[contenteditable]":"blur"}),this._options.initialCopyDirection||(this._options.initialCopyDirection=d.ModelBinder.Constants.ModelToView)},_initializeAttributeBindings:function(){var t,e,i,n;for(t in this._attributeBindings){for(e=this._attributeBindings[t],s.isString(e)?i={elementBindings:[{selector:e}]}:s.isArray(e)?i={elementBindings:e}:s.isObject(e)?i={elementBindings:[e]}:this._throwException("Unsupported type passed to Model Binder "+i),n=0;n<i.elementBindings.length;n++)i.elementBindings[n].attributeBinding=i;i.attributeName=t,this._attributeBindings[t]=i}},_initializeDefaultBindings:function(){var t,e,i,n,o;for(this._attributeBindings={},e=u("["+this._options.boundAttribute+"]",this._rootEl),t=0;t<e.length;t++)i=e[t],n=u(i).attr(this._options.boundAttribute),this._attributeBindings[n]?this._attributeBindings[n].elementBindings.push({attributeBinding:this._attributeBindings[n],boundEls:[i]}):((o={attributeName:n}).elementBindings=[{attributeBinding:o,boundEls:[i]}],this._attributeBindings[n]=o)},_initializeElBindings:function(){var t,e,i,n,o,s,r;for(t in this._attributeBindings)for(e=this._attributeBindings[t],i=0;i<e.elementBindings.length;i++)if(0===(o=""===(n=e.elementBindings[i]).selector?u(this._rootEl):u(n.selector,this._rootEl)).length)this._throwException("Bad binding found. No elements returned for binding selector "+n.selector);else for(n.boundEls=[],s=0;s<o.length;s++)r=o[s],n.boundEls.push(r)},_bindModelToView:function(){this._model.on("change",this._onModelChange,this),this._options.initialCopyDirection===d.ModelBinder.Constants.ModelToView&&this.copyModelAttributesToView()},copyModelAttributesToView:function(t){var e,i;for(e in this._attributeBindings)void 0!==t&&-1===s.indexOf(t,e)||(i=this._attributeBindings[e],this._copyModelToView(i))},copyViewValuesToModel:function(){var t,e,i,n,o,s;for(t in this._attributeBindings)for(e=this._attributeBindings[t],i=0;i<e.elementBindings.length;i++)if(n=e.elementBindings[i],this._isBindingUserEditable(n))if(this._isBindingRadioGroup(n))(s=this._getRadioButtonGroupCheckedEl(n))&&this._copyViewToModel(n,s);else for(o=0;o<n.boundEls.length;o++)s=u(n.boundEls[o]),this._isElUserEditable(s)&&this._copyViewToModel(n,s)},_unbindModelToView:function(){this._model&&(this._model.off("change",this._onModelChange),this._model=void 0)},_bindViewToModel:function(){s.each(this._options.changeTriggers,function(t,e){u(this._rootEl).on(t,e,this._onElChanged)},this),this._options.initialCopyDirection===d.ModelBinder.Constants.ViewToModel&&this.copyViewValuesToModel()},_unbindViewToModel:function(){this._options&&this._options.changeTriggers&&s.each(this._options.changeTriggers,function(t,e){u(this._rootEl).off(t,e,this._onElChanged)},this)},_onElChanged:function(t){var e,i,n,o;for(e=u(t.target)[0],i=this._getElBindings(e),n=0;n<i.length;n++)o=i[n],this._isBindingUserEditable(o)&&this._copyViewToModel(o,e)},_isBindingUserEditable:function(t){return void 0===t.elAttribute||"text"===t.elAttribute||"html"===t.elAttribute},_isElUserEditable:function(t){return t.attr("contenteditable")||t.is("input")||t.is("select")||t.is("textarea")},_isBindingRadioGroup:function(t){var e,i=0<t.boundEls.length;for(e=0;e<t.boundEls.length;e++)if("radio"!==u(t.boundEls[e]).attr("type")){i=!1;break}return i},_getRadioButtonGroupCheckedEl:function(t){var e,i;for(e=0;e<t.boundEls.length;e++)if("radio"===(i=u(t.boundEls[e])).attr("type")&&i.prop("checked"))return i},_getElBindings:function(t){var e,i,n,o,s,r=[];for(e in this._attributeBindings)for(i=this._attributeBindings[e],n=0;n<i.elementBindings.length;n++)for(o=i.elementBindings[n],s=0;s<o.boundEls.length;s++)o.boundEls[s]===t&&r.push(o);return r},_onModelChange:function(){var t,e;for(t in this._model.changedAttributes())(e=this._attributeBindings[t])&&this._copyModelToView(e)},_copyModelToView:function(t){var e,i,n,o,s,r;for(s=this._model.get(t.attributeName),e=0;e<t.elementBindings.length;e++)for(i=t.elementBindings[e],n=0;n<i.boundEls.length;n++)(o=i.boundEls[n])._isSetting||(r=this._getConvertedValue(d.ModelBinder.Constants.ModelToView,i,s),this._setEl(u(o),i,r))},_setEl:function(t,e,i){e.elAttribute?this._setElAttribute(t,e,i):this._setElValue(t,i)},_setElAttribute:function(t,e,i){switch(e.elAttribute){case"html":t.html(i);break;case"text":t.text(i);break;case"enabled":t.prop("disabled",!i);break;case"displayed":t[i?"show":"hide"]();break;case"hidden":t[i?"hide":"show"]();break;case"css":t.css(e.cssAttribute,i);break;case"class":var n=this._model.previous(e.attributeBinding.attributeName),o=this._model.get(e.attributeBinding.attributeName);s.isUndefined(n)&&s.isUndefined(o)||(n=this._getConvertedValue(d.ModelBinder.Constants.ModelToView,e,n),t.removeClass(n)),i&&t.addClass(i);break;default:t.attr(e.elAttribute,i)}},_setElValue:function(t,e){if(t.attr("type"))switch(t.attr("type")){case"radio":t.prop("checked",t.val()===e);break;case"checkbox":t.prop("checked",!!e);break;case"file":break;default:t.val(e)}else t.is("input")||t.is("select")||t.is("textarea")?t.val(e||(0===e?"0":"")):t.text(e||(0===e?"0":""))},_copyViewToModel:function(t,e){var i,n,o;e._isSetting||(e._isSetting=!0,i=this._setModel(t,u(e)),e._isSetting=!1,i&&t.converter&&(n=this._model.get(t.attributeBinding.attributeName),o=this._getConvertedValue(d.ModelBinder.Constants.ModelToView,t,n),this._setEl(u(e),t,o)))},_getElValue:function(t,e){switch(e.attr("type")){case"checkbox":return!!e.prop("checked");default:return void 0!==e.attr("contenteditable")?e.html():e.val()}},_setModel:function(t,e){var i={},n=this._getElValue(t,e);return n=this._getConvertedValue(d.ModelBinder.Constants.ViewToModel,t,n),i[t.attributeBinding.attributeName]=n,this._model.set(i,this._options.modelSetOptions)},_getConvertedValue:function(t,e,i){return e.converter?i=e.converter(t,i,e.attributeBinding.attributeName,this._model,e.boundEls):this._options.converter&&(i=this._options.converter(t,i,e.attributeBinding.attributeName,this._model,e.boundEls)),i},_throwException:function(t){if(!this._options.suppressThrows)throw new Error(t);"undefined"!=typeof console&&console.error&&console.error(t)}}),d.ModelBinder.CollectionConverter=function(t){if(this._collection=t,!this._collection)throw new Error("Collection must be defined");s.bindAll(this,"convert")},s.extend(d.ModelBinder.CollectionConverter.prototype,{convert:function(t,e){return t===d.ModelBinder.Constants.ModelToView?e?e.id:void 0:this._collection.get(e)}}),d.ModelBinder.createDefaultBindings=function(t,e,i,n){var o,s,r,d,l={};for(o=u("["+e+"]",t),s=0;s<o.length;s++)if(r=o[s],!l[d=u(r).attr(e)]){var a={selector:"["+e+'="'+d+'"]'};l[d]=a,i&&(l[d].converter=i),n&&(l[d].elAttribute=n)}return l},d.ModelBinder.combineBindings=function(n,t){return s.each(t,function(t,e){var i={selector:t.selector};t.converter&&(i.converter=t.converter),t.elAttribute&&(i.elAttribute=t.elAttribute),n[e]?n[e]=[n[e],i]:n[e]=i}),n},d.ModelBinder});