-
Notifications
You must be signed in to change notification settings - Fork 67
/
backbone.modelbinding.min.js
25 lines (25 loc) · 6.94 KB
/
backbone.modelbinding.min.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
// Backbone.ModelBinding v0.5.0
//
// Copyright (C)2011 Derick Bailey, Muted Solutions, LLC
// Distributed Under MIT Liscene
//
// Documentation and Full Licence Availabe at:
// http://github.com/derickbailey/backbone.modelbinding
//
// ----------------------------
// Backbone.ModelBinding
// ----------------------------
(function(m){var p=function(j,l,r){var c={version:"0.5.0",bind:function(a,b){a.modelBinder=new m(a,b);a.modelBinder.bind()},unbind:function(a){a.modelBinder&&a.modelBinder.unbind()}},m=function(a,b){this.config=new c.Configuration(b);this.modelBindings=[];this.elementBindings=[];this.bind=function(){var q=c.Conventions,b;for(b in q)if(q.hasOwnProperty(b)){var f=q[b];f.handler.bind.call(this,f.selector,a,a.model,this.config)}};this.unbind=function(){l.each(this.elementBindings,function(b){b.element.unbind(b.eventName,
b.callback)});l.each(this.modelBindings,function(b){b.model.unbind(b.eventName,b.callback)})};this.registerModelBinding=function(b,a,c){a="change:"+a;b.bind(a,c);this.modelBindings.push({model:b,eventName:a,callback:c})};this.registerDataBinding=function(b,a,c){b.bind(a,c);this.modelBindings.push({model:b,eventName:a,callback:c})};this.registerElementBinding=function(b,a){b.bind("change",a);this.elementBindings.push({element:b,eventName:"change",callback:a})}};c.Configuration=function(a){this.bindingAttrConfig=
{};l.extend(this.bindingAttrConfig,c.Configuration.bindindAttrConfig,a);if(this.bindingAttrConfig.all){a=this.bindingAttrConfig.all;delete this.bindingAttrConfig.all;for(var b in this.bindingAttrConfig)this.bindingAttrConfig.hasOwnProperty(b)&&(this.bindingAttrConfig[b]=a)}this.getBindingAttr=function(b){return this.bindingAttrConfig[b]};this.getBindingValue=function(b,a){var c=this.getBindingAttr(a);return b.attr(c)}};c.Configuration.bindindAttrConfig={text:"id",textarea:"id",password:"id",radio:"name",
checkbox:"id",select:"id",number:"id",range:"id",tel:"id",search:"id",url:"id",email:"id"};c.Configuration.store=function(){c.Configuration.originalConfig=l.clone(c.Configuration.bindindAttrConfig)};c.Configuration.restore=function(){c.Configuration.bindindAttrConfig=c.Configuration.originalConfig};c.Configuration.configureBindingAttributes=function(a){a.all&&(this.configureAllBindingAttributes(a.all),delete a.all);l.extend(c.Configuration.bindindAttrConfig,a)};c.Configuration.configureAllBindingAttributes=
function(a){var b=c.Configuration.bindindAttrConfig;b.text=a;b.textarea=a;b.password=a;b.radio=a;b.checkbox=a;b.select=a;b.number=a;b.range=a;b.tel=a;b.search=a;b.url=a;b.email=a};var k=function(){var a={},b=function(b){var a=b[0].tagName.toLowerCase();if("input"==a&&(a=b.attr("type"),void 0==a||""==a))a="text";return a};a.bind=function(a,c,f,n){var g=this;c.$(a).each(function(){var a=c.$(this),d=b(a),e=n.getBindingValue(a,d),o=function(a,b){var d={};d[a]=b;f.set(d)};g.registerModelBinding(f,e,function(b,
d){a.val(d)});g.registerElementBinding(a,function(b){o(e,c.$(b.target).val())});d=f.get(e);"undefined"!==typeof d&&null!==d?a.val(d):(d=a.val())&&o(e,d)})};return a}(j),p=function(){return{bind:function(a,b,c,i){var f=this;b.$(a).each(function(){var a=b.$(this),g=i.getBindingValue(a,"select"),h=function(a,b,d){var e={};e[a]=b;e[a+"_text"]=d;c.set(e)};f.registerModelBinding(c,g,function(b,d){a.val(d)});f.registerElementBinding(a,function(a){var d=b.$(a.target),a=d.val(),d=d.find(":selected").text();
h(g,a,d)});var d=c.get(g);"undefined"!==typeof d&&null!==d&&a.val(d);if(a.val()!=d){var d=a.val(),e=a.find(":selected").text();h(g,d,e)}})}}}(j),s=function(){return{bind:function(a,b,c,i){var f=this,n=[];b.$(a).each(function(){var a=b.$(this),h=i.getBindingValue(a,"radio");if(!n[h]){n[h]=!0;var d=i.getBindingAttr("radio");f.registerModelBinding(c,h,function(a,c){b.$("input[type=radio]["+d+"='"+h+"'][value='"+c+"']").attr("checked","checked")});var e=function(a,b){var d={};d[a]=b;c.set(d)},o=function(a){a=
b.$(a.currentTarget);a.is(":checked")&&e(h,a.val())};b.$("input[type=radio]["+d+"='"+h+"']").each(function(){var a=r(this);f.registerElementBinding(a,o)});a=c.get(h);"undefined"!==typeof a&&null!==a?b.$("input[type=radio]["+d+"='"+h+"'][value='"+a+"']").attr("checked","checked"):(a=b.$("input[type=radio]["+d+"='"+h+"']:checked").val(),e(h,a))}})}}}(j),t=function(){return{bind:function(a,b,c,i){var f=this;b.$(a).each(function(){var a=b.$(this);i.getBindingAttr("checkbox");var g=i.getBindingValue(a,
"checkbox"),h=function(a,b){var d={};d[a]=b;c.set(d)};f.registerModelBinding(c,g,function(b,d){d?a.attr("checked","checked"):a.removeAttr("checked")});f.registerElementBinding(a,function(a){a=b.$(a.target).is(":checked")?!0:!1;h(g,a)});if(c.attributes.hasOwnProperty(g)){var d=c.get(g);"undefined"!==typeof d&&null!==d&&!1!=d?a.attr("checked","checked"):a.removeAttr("checked")}else d=a.is(":checked")?!0:!1,h(g,d)})}}}(j),j=function(a,b,j){var i={"default":""};c.Configuration.dataBindSubst=function(a){this.storeDataBindSubstConfig();
b.extend(i,a)};c.Configuration.storeDataBindSubstConfig=function(){c.Configuration._dataBindSubstConfig=b.clone(i)};c.Configuration.restoreDataBindSubstConfig=function(){c.Configuration._dataBindSubstConfig&&(i=c.Configuration._dataBindSubstConfig,delete c.Configuration._dataBindSubstConfig)};c.Configuration.getDataBindSubst=function(a,b){var c=b;void 0===b&&(c=i.hasOwnProperty(a)?i[a]:i["default"]);return c};var f=function(a,b,e){e=c.Configuration.getDataBindSubst(b,e);switch(b){case "html":a.html(e);
break;case "text":a.text(e);break;case "enabled":a.attr("disabled",!e);break;case "displayed":a[e?"show":"hide"]();break;case "hidden":a[e?"hide":"show"]();break;default:a.attr(b,e)}},k=function(a){var d=[],e=c.Conventions.databind.selector.replace(/^(.*\[)([^\]]*)(].*)/g,"$2"),a=a.attr(e).split(";");b.each(a,function(a){a=j.trim(a).split(" ");1==a.length&&a.unshift("text");d.push({elementAttr:a[0],modelAttr:a[1]})});return d},g=function(a,b){var c={},g=b.modelAttr;0==g.indexOf("event:")?(c.name=
g.substr(6),c.callback=function(c){f(a,b.elementAttr,c)}):(c.name="change:"+g,c.callback=function(c,e){f(a,b.elementAttr,e)});return c};return{bind:function(a,c,e){var i=this;c.$(a).each(function(){var a=c.$(this),h=k(a);b.each(h,function(b){var c=g(a,b);i.registerDataBinding(e,c.name,c.callback);f(a,b.elementAttr,e.get(b.modelAttr))})})}}}(j,l,r);c.Conventions={text:{selector:"input:text",handler:k},textarea:{selector:"textarea",handler:k},password:{selector:"input:password",handler:k},radio:{selector:"input:radio",
handler:s},checkbox:{selector:"input:checkbox",handler:t},select:{selector:"select",handler:p},databind:{selector:"*[data-bind]",handler:j},number:{selector:"input[type=number]",handler:k},range:{selector:"input[type=range]",handler:k},tel:{selector:"input[type=tel]",handler:k},search:{selector:"input[type=search]",handler:k},url:{selector:"input[type=url]",handler:k},email:{selector:"input[type=email]",handler:k}};return c};"function"===typeof define&&define.amd?define(["backbone","underscore","jquery"],
function(j,l,m){return p(j,l,m)}):(m.Backbone=Backbone||{},m.Backbone.ModelBinding=p(Backbone,_,jQuery))})(this);