Skip to content

Commit

Permalink
initial commit, moving bind.js from https://github.com/remy/libraries…
Browse files Browse the repository at this point in the history
  • Loading branch information
remy committed Jun 6, 2013
0 parents commit 114e0f6
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
Empty file added README.md
Empty file.
105 changes: 105 additions & 0 deletions bind.js
@@ -0,0 +1,105 @@
/*
Simple data to HTML binding.
Requires:
setters/gettings, bind, qSA, getOwnPropertyNames, forEach - basically a decent browser
Arguments:
- obj: this is your data structure that is copied in to the Bind
- mapping: this is a key/value pair of object path to DOM selector
Example:
var data = Bind({
me: {
name: "@rem",
score: 11
}
}, {
'me.score': '#score',
'me.name': '#name'
});
Restrictions:
Only supports simple changes to the object - if you change the structure, then the bindings are lost.
i.e. this won't work:
var data = Bind({...}, {...});
data.me.name = '@leftlogic'; //works
data.me = { // fails
name: '@rem'
}
*/
var Bind = (function () {
"use strict"; // damn jshint & sublime

var $ = document.querySelectorAll.bind(document),
forEach = [].forEach;

function extend(target, object, mapping, _path) {
if (!_path) { _path = []; }
Object.getOwnPropertyNames(object).forEach(function (key) {
var value = object[key];
var path = [].slice.call(_path);
path.push(key);

var elements;

if (typeof value === "object" && value !== null && !Array.isArray(value)) {
target[key] = extend(target[key] || {}, value, mapping, path);
} else {
var callback;
if (typeof mapping[path.join('.')] === 'function') {
callback = mapping[path.join('.')];
} else {
callback = function (value) {
if (callback.elements) {
forEach.call(callback.elements, function (element) {
element.innerHTML = value;
});
}
};
callback.elements = $(mapping[path.join('.')] || '☺');
}

Object.defineProperty(target, key, {
set: function (v) {
if (typeof v === "object" && v !== null && !Array.isArray(v)) {
target[key] = extend(target[key] || {}, v, mapping, path);
} else {
value = v;
}

if (callback) {
callback(value);
}
},
get: function () {
return value;
}
});

target[key] = value;
}
});
return target;
}

var Bind = function (obj, mapping) {
if (!this || this === window) {
return new Bind(obj, mapping);
}
extend(this, obj, mapping);
return this;
};

return Bind;

})();

0 comments on commit 114e0f6

Please sign in to comment.