b1b0aa9a by Michael Richards

Implement full node/attribute parsing.

1 parent a8c387d3
window.rivets = (function() {
var attr, bindableAttributes, bindings, getInputValue, registerBinding, setAttribute, _fn, _i, _len;
registerBinding = function(el, interface, contexts, type, callback) {
return $("*[data-" + type + "]", el).each(function() {
var context, inputValue, keypath, path;
var _this = this;
path = $(this).attr("data-" + type).split('.');
context = path.shift();
keypath = path.join('.');
callback(this, interface.read(contexts[context], keypath));
interface.subscribe(contexts[context], keypath, function(value) {
return callback(_this, value);
});
if (inputValue = getInputValue(this)) {
return interface.publish(contexts[context], keypath, inputValue);
}
registerBinding = function(el, interface, type, context, keypath) {
var inputValue;
bindings[type](el, interface.read(context, keypath));
interface.subscribe(context, keypath, function(value) {
return bindings[type](el, value);
});
if (inputValue = getInputValue(el)) {
return interface.publish(context, keypath, inputValue);
}
};
setAttribute = function(el, attr, value, mirrored) {
if (mirrored == null) mirrored = false;
......@@ -88,14 +82,31 @@
}
return {
bind: function(el, interface, contexts) {
var callback, type, _results;
if (contexts == null) contexts = {};
_results = [];
for (type in bindings) {
callback = bindings[type];
_results.push(registerBinding(el, interface, contexts, type, callback));
}
return _results;
return $(el).add($('*', el)).each(function() {
var nodeMap, target, _j, _ref, _results;
target = this;
nodeMap = target.attributes;
if (nodeMap.length > 0) {
return (function() {
_results = [];
for (var _j = 0, _ref = nodeMap.length - 1; 0 <= _ref ? _j <= _ref : _j >= _ref; 0 <= _ref ? _j++ : _j--){ _results.push(_j); }
return _results;
}).apply(this).forEach(function(n) {
var context, keypath, node, path, type;
node = nodeMap[n];
if (/^data-/.test(node.name)) {
type = node.name.replace('data-', '');
if (_.include(_.keys(bindings), type)) {
path = node.value.split('.');
context = path.shift();
keypath = path.join('.');
return registerBinding($(target), interface, type, contexts[context], keypath);
}
}
});
}
});
}
};
})();
......
......@@ -4,19 +4,14 @@
# license : MIT
window.rivets = do ->
registerBinding = (el, interface, contexts, type, callback) ->
$("*[data-#{type}]", el).each ->
path = $(this).attr("data-#{type}").split '.'
context = path.shift()
keypath = path.join '.'
registerBinding = (el, interface, type, context, keypath) ->
bindings[type] el, interface.read(context, keypath)
callback this, interface.read contexts[context], keypath
interface.subscribe context, keypath, (value) ->
bindings[type] el, value
interface.subscribe contexts[context], keypath, (value) =>
callback this, value
if inputValue = getInputValue this
interface.publish contexts[context], keypath, inputValue
if inputValue = getInputValue el
interface.publish context, keypath, inputValue
setAttribute = (el, attr, value, mirrored=false) ->
if value
......@@ -59,5 +54,19 @@ window.rivets = do ->
setAttribute el, attr, value
bind: (el, interface, contexts={}) ->
for type, callback of bindings
registerBinding el, interface, contexts, type, callback
\ No newline at end of file
$(el).add($('*', el)).each ->
target = this
nodeMap = target.attributes
if nodeMap.length > 0
[0..(nodeMap.length - 1)].forEach (n) ->
node = nodeMap[n]
if /^data-/.test node.name
type = node.name.replace 'data-', ''
if _.include _.keys(bindings), type
path = node.value.split '.'
context = path.shift()
keypath = path.join '.'
registerBinding $(target), interface, type, contexts[context], keypath
......