b1b0aa9a by Michael Richards

Implement full node/attribute parsing.

1 parent a8c387d3
1 1
2 window.rivets = (function() { 2 window.rivets = (function() {
3 var attr, bindableAttributes, bindings, getInputValue, registerBinding, setAttribute, _fn, _i, _len; 3 var attr, bindableAttributes, bindings, getInputValue, registerBinding, setAttribute, _fn, _i, _len;
4 registerBinding = function(el, interface, contexts, type, callback) { 4 registerBinding = function(el, interface, type, context, keypath) {
5 return $("*[data-" + type + "]", el).each(function() { 5 var inputValue;
6 var context, inputValue, keypath, path; 6 bindings[type](el, interface.read(context, keypath));
7 var _this = this; 7 interface.subscribe(context, keypath, function(value) {
8 path = $(this).attr("data-" + type).split('.'); 8 return bindings[type](el, value);
9 context = path.shift();
10 keypath = path.join('.');
11 callback(this, interface.read(contexts[context], keypath));
12 interface.subscribe(contexts[context], keypath, function(value) {
13 return callback(_this, value);
14 });
15 if (inputValue = getInputValue(this)) {
16 return interface.publish(contexts[context], keypath, inputValue);
17 }
18 }); 9 });
10 if (inputValue = getInputValue(el)) {
11 return interface.publish(context, keypath, inputValue);
12 }
19 }; 13 };
20 setAttribute = function(el, attr, value, mirrored) { 14 setAttribute = function(el, attr, value, mirrored) {
21 if (mirrored == null) mirrored = false; 15 if (mirrored == null) mirrored = false;
...@@ -88,14 +82,31 @@ ...@@ -88,14 +82,31 @@
88 } 82 }
89 return { 83 return {
90 bind: function(el, interface, contexts) { 84 bind: function(el, interface, contexts) {
91 var callback, type, _results;
92 if (contexts == null) contexts = {}; 85 if (contexts == null) contexts = {};
93 _results = []; 86 return $(el).add($('*', el)).each(function() {
94 for (type in bindings) { 87 var nodeMap, target, _j, _ref, _results;
95 callback = bindings[type]; 88 target = this;
96 _results.push(registerBinding(el, interface, contexts, type, callback)); 89 nodeMap = target.attributes;
97 } 90 if (nodeMap.length > 0) {
98 return _results; 91 return (function() {
92 _results = [];
93 for (var _j = 0, _ref = nodeMap.length - 1; 0 <= _ref ? _j <= _ref : _j >= _ref; 0 <= _ref ? _j++ : _j--){ _results.push(_j); }
94 return _results;
95 }).apply(this).forEach(function(n) {
96 var context, keypath, node, path, type;
97 node = nodeMap[n];
98 if (/^data-/.test(node.name)) {
99 type = node.name.replace('data-', '');
100 if (_.include(_.keys(bindings), type)) {
101 path = node.value.split('.');
102 context = path.shift();
103 keypath = path.join('.');
104 return registerBinding($(target), interface, type, contexts[context], keypath);
105 }
106 }
107 });
108 }
109 });
99 } 110 }
100 }; 111 };
101 })(); 112 })();
......
...@@ -4,19 +4,14 @@ ...@@ -4,19 +4,14 @@
4 # license : MIT 4 # license : MIT
5 5
6 window.rivets = do -> 6 window.rivets = do ->
7 registerBinding = (el, interface, contexts, type, callback) -> 7 registerBinding = (el, interface, type, context, keypath) ->
8 $("*[data-#{type}]", el).each -> 8 bindings[type] el, interface.read(context, keypath)
9 path = $(this).attr("data-#{type}").split '.'
10 context = path.shift()
11 keypath = path.join '.'
12 9
13 callback this, interface.read contexts[context], keypath 10 interface.subscribe context, keypath, (value) ->
11 bindings[type] el, value
14 12
15 interface.subscribe contexts[context], keypath, (value) => 13 if inputValue = getInputValue el
16 callback this, value 14 interface.publish context, keypath, inputValue
17
18 if inputValue = getInputValue this
19 interface.publish contexts[context], keypath, inputValue
20 15
21 setAttribute = (el, attr, value, mirrored=false) -> 16 setAttribute = (el, attr, value, mirrored=false) ->
22 if value 17 if value
...@@ -59,5 +54,19 @@ window.rivets = do -> ...@@ -59,5 +54,19 @@ window.rivets = do ->
59 setAttribute el, attr, value 54 setAttribute el, attr, value
60 55
61 bind: (el, interface, contexts={}) -> 56 bind: (el, interface, contexts={}) ->
62 for type, callback of bindings
63 registerBinding el, interface, contexts, type, callback
...\ No newline at end of file ...\ No newline at end of file
57 $(el).add($('*', el)).each ->
58 target = this
59 nodeMap = target.attributes
60
61 if nodeMap.length > 0
62 [0..(nodeMap.length - 1)].forEach (n) ->
63 node = nodeMap[n]
64
65 if /^data-/.test node.name
66 type = node.name.replace 'data-', ''
67
68 if _.include _.keys(bindings), type
69 path = node.value.split '.'
70 context = path.shift()
71 keypath = path.join '.'
72 registerBinding $(target), interface, type, contexts[context], keypath
......