Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 79 additions & 53 deletions dist/simpleBind.2.0.js
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -92,31 +116,31 @@ 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;
};

// 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]];
Expand All @@ -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;
Expand All @@ -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(':');
}
Expand All @@ -170,23 +194,24 @@ 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);
}
};

return pub;
})(document,{});
})(document, {});

simpleBind = (function(w,d,util,pub){
var state = {
bindTypes: [ ],
Expand Down Expand Up @@ -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();

Expand Down
Loading