diff --git a/dist/simpleBind.2.0.js b/dist/simpleBind.2.0.js index 57f4083..9e1ba8f 100644 --- a/dist/simpleBind.2.0.js +++ b/dist/simpleBind.2.0.js @@ -1,75 +1,99 @@ var simpleBind = (function(sb){ return sb; })(simpleBind||{}); -simpleBind.util = (function(d,pub){ +simpleBind.util = (function (d, pub) { - var getType = function(variable) { + var getType = function (variable) { var type = typeof variable; - switch(type) { + switch (type) { case 'object': - if(variable == null) return 'null'; + if (variable == null) return 'null'; return variable instanceof Array ? 'array' : type; default: return type; } }; - var extend = function(args) { - for(var i=1, len = args.length; i < len; ++i) { + var copyArrayWithoutReferences = function (arr) { + var toReturn = []; + for (var i = 0; i < arr.length; ++i) { + var type = getType(arr[i]); + switch (type) { + case 'object': + toReturn.push(pub.extend({}, arr[i])); + break; + case 'array': + toReturn.push(copyArrayWithoutReferences(arr[i])); + break; + default: + toReturn.push(arr[i]); + break; + } + } + return toReturn; + }; + + var extend = function (args) { + for (var i = 1, len = args.length; i < len; ++i) { var src = args[i] , target = args[0]; - for(var key in src) { - var simpleExtend = getType(target[key]) != 'object' && getType(src[key]) != 'object'; - if(simpleExtend) { + for (var key in src) { + var isArrayPresent = getType(src[key]) === 'array'; + var simpleExtend = getType(target[key]) != 'object' && getType(src[key]) != 'object' && !isArrayPresent; + if (simpleExtend) { target[key] = src[key]; } else { - target[key] = pub.extend(typeof target[key] == 'undefined' ? { } : target[key],src[key]); + if (isArrayPresent) { + target[key] = copyArrayWithoutReferences(src[key]); + } else { + target[key] = pub.extend(typeof target[key] == 'undefined' ? {} : target[key], src[key]); + } } } } - return args.length ? args[0] : { }; + return args.length ? args[0] : {}; }; - pub.delay = (function(){ + pub.delay = (function () { var timer = 0; - return function(callback, ms){ - clearTimeout (timer); + return function (callback, ms) { + clearTimeout(timer); timer = setTimeout(callback, ms); }; })(); - pub.extend = function() { + pub.extend = function () { return extend(arguments); }; - pub.getKeys = function(obj) { - if(Object.keys) { + pub.getKeys = function (obj) { + if (Object.keys) { return Object.keys(obj); } else { arr = []; - for(var key in obj) { + for (var key in obj) { arr.push(key); } return arr; } }; - pub.getData = function(elem) { - var attrs, keys, data = { }; + pub.getData = function (elem) { + var attrs, keys, data = {}; attrs = pub.getAttrs(elem); keys = pub.getKeys(attrs); - for(var i=0; i < keys.length; ++i) { - if(keys[i].indexOf('data-') === 0) { - data[keys[i].substring(5,keys[i].length)] = attrs[keys[i]]; + for (var i = 0; i < keys.length; ++i) { + if (keys[i].indexOf('data-') === 0) { + data[keys[i].substring(5, keys[i].length)] = attrs[keys[i]]; } } return data; }; - pub.getAttrs = function(elem) { + pub.getAttrs = function (elem) { var attrs, obj = {}; attrs = elem.attributes; - for(var i=0; i < attrs.length; ++i) { + for (var i = 0; i < attrs.length; ++i) { var attr = attrs.item(i); obj[attr.nodeName] = (attr.hasOwnProperty('value')) ? attr.value : attr.nodeValue; } @@ -92,12 +116,12 @@ simpleBind.util = (function(d,pub){ // build an object so we could have SEO friendly // binds and so that people don't have to rush // to bind their objects immediately - pub.set = function(obj,str,val) { + pub.set = function (obj, str, val) { str = str.split('.'); var finalProp = str.pop(); - while(str.length) { + while (str.length) { var key = str.shift(); - obj[key] = typeof obj[key] == 'undefined' ? { } : obj[key]; + obj[key] = typeof obj[key] == 'undefined' ? {} : obj[key]; obj = obj[key]; } obj[finalProp] = val; @@ -105,18 +129,18 @@ simpleBind.util = (function(d,pub){ // Same as above but retrieves the value as a string or as an empty string // if not set: - pub.get = function(obj,str) { - if(str == '$base' || str === '') return obj; + pub.get = function (obj, str) { + if (str == '$base' || str === '') return obj; str = str.split('.'); - for(var i=0; i < str.length; ++i) { - if(str[i] == '$base') { + for (var i = 0; i < str.length; ++i) { + if (str[i] == '$base') { return obj; - } else if(obj == null) { + } else if (obj == null) { return ''; - } else if(typeof obj[str[i]] == 'undefined') { + } else if (typeof obj[str[i]] == 'undefined') { return ''; } else { - if(obj === null) { + if (obj === null) { return ''; } else { obj = obj[str[i]]; @@ -127,29 +151,29 @@ simpleBind.util = (function(d,pub){ }; var standardObjNameRegex = new RegExp(/^[^\.]*/); - var replaceObjNameInStandardFormat = function(str,oldObj,newObj) { - if(str == oldObj) { + var replaceObjNameInStandardFormat = function (str, oldObj, newObj) { + if (str == oldObj) { str = newObj; - } else if(str.indexOf(oldObj + '.') == 0) { - str = str.replace(standardObjNameRegex,newObj) + } else if (str.indexOf(oldObj + '.') == 0) { + str = str.replace(standardObjNameRegex, newObj) }; return str; }; - pub.replaceObjNameInBindingStr = function(str,bindType,oldObj,newObj) { + pub.replaceObjNameInBindingStr = function (str, bindType, oldObj, newObj) { var origStr = str + ''; - if(str.indexOf(':') > -1) { + if (str.indexOf(':') > -1) { // we have either a bindhandler, simpledata, simpleevent, simplebindattrs, or simplerepeat - switch(bindType) { + switch (bindType) { case 'simplerepeat': str = str.split(':'); - str[1] = replaceObjNameInStandardFormat(str[1],oldObj,newObj); + str[1] = replaceObjNameInStandardFormat(str[1], oldObj, newObj); str = str.join(':'); break; case 'simpleevent': str = str.split(':'); - if(str.length >= 3) { - str[2] = replaceObjNameInStandardFormat(str[2],oldObj,newObj); + if (str.length >= 3) { + str[2] = replaceObjNameInStandardFormat(str[2], oldObj, newObj); } str = str.join(':'); break; @@ -158,10 +182,10 @@ simpleBind.util = (function(d,pub){ case 'simplebindattrs': default: str = str.split(','); - for(var i=0; i < str.length; ++i) { + for (var i = 0; i < str.length; ++i) { str[i] = str[i].split(':'); - if(str[i].length > 1) { - str[i][1] = replaceObjNameInStandardFormat(str[i][1],oldObj,newObj) + if (str[i].length > 1) { + str[i][1] = replaceObjNameInStandardFormat(str[i][1], oldObj, newObj) }; str[i] = str[i].join(':'); } @@ -170,15 +194,15 @@ simpleBind.util = (function(d,pub){ } } else { // we have a simplebind or simplebindvalue - str = replaceObjNameInStandardFormat(str,oldObj,newObj); + str = replaceObjNameInStandardFormat(str, oldObj, newObj); } return str; }; - pub.triggerEvent = function(elem,type){ - if('createEvent' in d) { + pub.triggerEvent = function (elem, type) { + if ('createEvent' in d) { var evt = d.createEvent('HTMLEvents'); - evt.initEvent(type,false,true); + evt.initEvent(type, false, true); elem.dispatchEvent(evt); } else { elem.fireEvent('on' + type); @@ -186,7 +210,8 @@ simpleBind.util = (function(d,pub){ }; return pub; -})(document,{}); +})(document, {}); + simpleBind = (function(w,d,util,pub){ var state = { bindTypes: [ ], @@ -390,6 +415,7 @@ simpleBind = (function(w,d,util,pub){ return pub; })(window,document,simpleBind.util,simpleBind||{}); + simpleBind = (function(w,d,util,pub){ var state = pub.getState(); diff --git a/dist/simpleBind.2.0.min.js b/dist/simpleBind.2.0.min.js index c61f397..1574df8 100644 --- a/dist/simpleBind.2.0.min.js +++ b/dist/simpleBind.2.0.min.js @@ -1 +1 @@ -var simpleBind=function(e){return e}(simpleBind||{});simpleBind.util=function(e,t){var n=function(e){var t=typeof e;switch(t){case"object":return null==e?"null":e instanceof Array?"array":t;default:return t}},i=function(e){for(var i=1,r=e.length;i-1)switch(t){case"simplerepeat":e=e.split(":"),e[1]=d(e[1],n,i),e=e.join(":");break;case"simpleevent":e=e.split(":"),e.length>=3&&(e[2]=d(e[2],n,i)),e=e.join(":");break;case"simplebindhandler":case"simpledata":case"simplebindattrs":default:e=e.split(",");for(var r=0;r1&&(e[r][1]=d(e[r][1],n,i)),e[r]=e[r].join(":");e=e.join(",")}else e=d(e,n,i);return e},t.triggerEvent=function(t,n){if("createEvent"in e){var i=e.createEvent("HTMLEvents");i.initEvent(n,!1,!0),t.dispatchEvent(i)}else t.fireEvent("on"+n)},t}(document,{}),simpleBind=function(e,t,n,i){var r={bindTypes:[],bindTypeOpts:{},boundElems:{},boundObjects:{},boundObjectsLast:{},ready:!1,beforeReadyBindQueue:[],autoReBinding:!1,autoReBindingQueue:{}},d=function(){if(a(),r.ready=!0,r.beforeReadyBindQueue.length)for(var e=0;e-1){var l=r.repeatDictionary[t.split("-").shift()];i.bind(l,r.boundObjects[l])}}},o=function(){var e=this;n.delay(function(){l.call(e)},50)},s=function(e){var t,n=e.tagName.toLowerCase();switch(n){case"input":return t=e.getAttribute("type");default:return n}},u=function(e,t){var n=s(e);switch(n){case"checkbox":return String(e.checked)!=String(t);case"radio":var i=p(e);return e.checked&&i!=String(t)||!e.checked&&i==String(t);default:return t!=p(e)}},p=function(e){var t=s(e);switch(t){case"checkbox":return e.checked;default:return e.value}},f=function(e,t){switch(t){case"text":case"tel":case"password":case"textarea":case"zip":e.addEventListener("keyup",o);default:e.addEventListener("change",l)}},b=function(e,t){switch(e.inputType){case"select":for(var n=e.elem.getElementsByTagName("option"),i=0,r=0;r-1)switch(t){case"simplerepeat":e=e.split(":"),e[1]=a(e[1],n,i),e=e.join(":");break;case"simpleevent":e=e.split(":"),e.length>=3&&(e[2]=a(e[2],n,i)),e=e.join(":");break;case"simplebindhandler":case"simpledata":case"simplebindattrs":default:e=e.split(",");for(var r=0;r1&&(e[r][1]=a(e[r][1],n,i)),e[r]=e[r].join(":");e=e.join(",")}else e=a(e,n,i);return e},t.triggerEvent=function(t,n){if("createEvent"in e){var i=e.createEvent("HTMLEvents");i.initEvent(n,!1,!0),t.dispatchEvent(i)}else t.fireEvent("on"+n)},t}(document,{}),simpleBind=function(e,t,n,i){var r={bindTypes:[],bindTypeOpts:{},boundElems:{},boundObjects:{},boundObjectsLast:{},ready:!1,beforeReadyBindQueue:[],autoReBinding:!1,autoReBindingQueue:{}},d=function(){if(a(),r.ready=!0,r.beforeReadyBindQueue.length)for(var e=0;e-1){var s=r.repeatDictionary[t.split("-").shift()];i.bind(s,r.boundObjects[s])}}},o=function(){var e=this;n.delay(function(){s.call(e)},50)},l=function(e){var t,n=e.tagName.toLowerCase();switch(n){case"input":return t=e.getAttribute("type");default:return n}},u=function(e,t){var n=l(e);switch(n){case"checkbox":return String(e.checked)!=String(t);case"radio":var i=p(e);return e.checked&&i!=String(t)||!e.checked&&i==String(t);default:return t!=p(e)}},p=function(e){var t=l(e);switch(t){case"checkbox":return e.checked;default:return e.value}},f=function(e,t){switch(t){case"text":case"tel":case"password":case"textarea":case"zip":e.addEventListener("keyup",o);default:e.addEventListener("change",s)}},b=function(e,t){switch(e.inputType){case"select":for(var n=e.elem.getElementsByTagName("option"),i=0,r=0;r -1) { + if (str.indexOf(':') > -1) { // we have either a bindhandler, simpledata, simpleevent, simplebindattrs, or simplerepeat - switch(bindType) { + switch (bindType) { case 'simplerepeat': str = str.split(':'); - str[1] = replaceObjNameInStandardFormat(str[1],oldObj,newObj); + str[1] = replaceObjNameInStandardFormat(str[1], oldObj, newObj); str = str.join(':'); break; case 'simpleevent': str = str.split(':'); - if(str.length >= 3) { - str[2] = replaceObjNameInStandardFormat(str[2],oldObj,newObj); + if (str.length >= 3) { + str[2] = replaceObjNameInStandardFormat(str[2], oldObj, newObj); } str = str.join(':'); break; @@ -155,10 +179,10 @@ simpleBind.util = (function(d,pub){ case 'simplebindattrs': default: str = str.split(','); - for(var i=0; i < str.length; ++i) { + for (var i = 0; i < str.length; ++i) { str[i] = str[i].split(':'); - if(str[i].length > 1) { - str[i][1] = replaceObjNameInStandardFormat(str[i][1],oldObj,newObj) + if (str[i].length > 1) { + str[i][1] = replaceObjNameInStandardFormat(str[i][1], oldObj, newObj) }; str[i] = str[i].join(':'); } @@ -167,15 +191,15 @@ simpleBind.util = (function(d,pub){ } } else { // we have a simplebind or simplebindvalue - str = replaceObjNameInStandardFormat(str,oldObj,newObj); + str = replaceObjNameInStandardFormat(str, oldObj, newObj); } return str; }; - pub.triggerEvent = function(elem,type){ - if('createEvent' in d) { + pub.triggerEvent = function (elem, type) { + if ('createEvent' in d) { var evt = d.createEvent('HTMLEvents'); - evt.initEvent(type,false,true); + evt.initEvent(type, false, true); elem.dispatchEvent(evt); } else { elem.fireEvent('on' + type); @@ -183,4 +207,4 @@ simpleBind.util = (function(d,pub){ }; return pub; -})(document,{}); \ No newline at end of file +})(document, {});