f093f839 by Michael Richards

Move value parsing logic into a Rivets.View class (used to parse and store data …

…for the binded view).
1 parent cd0fca36
......@@ -49,6 +49,64 @@
})();
Rivets.View = (function() {
View.name = 'View';
function View(el, contexts) {
this.el = el;
this.contexts = contexts;
this.bind = __bind(this.bind, this);
this.build = __bind(this.build, this);
this.build();
}
View.prototype.build = function() {
var attribute, context, keypath, node, path, type, _i, _len, _ref, _results;
this.bindings = [];
_ref = this.el.getElementsByTagName('*');
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
node = _ref[_i];
_results.push((function() {
var _j, _len1, _ref1, _results1;
_ref1 = node.attributes;
_results1 = [];
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
attribute = _ref1[_j];
if (/^data-/.test(attribute.name)) {
type = attribute.name.replace('data-', '');
path = attribute.value.split('.');
context = this.contexts[path.shift()];
keypath = path.join('.');
_results1.push(this.bindings.push(new Rivets.Binding(node, type, context, keypath)));
} else {
_results1.push(void 0);
}
}
return _results1;
}).call(this));
}
return _results;
};
View.prototype.bind = function() {
var binding, _i, _len, _ref, _results;
_ref = this.bindings;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
binding = _ref[_i];
_results.push(binding.bind());
}
return _results;
};
return View;
})();
getInputValue = function(el) {
switch (el.type) {
case 'text':
......@@ -130,31 +188,13 @@
return Rivets.bindings[routine] = routineFunction;
},
bind: function(el, contexts) {
var attribute, binding, bindings, context, keypath, node, path, type, _i, _j, _k, _len, _len1, _len2, _ref, _ref1;
var view;
if (contexts == null) {
contexts = {};
}
bindings = [];
_ref = el.getElementsByTagName('*');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
node = _ref[_i];
_ref1 = node.attributes;
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
attribute = _ref1[_j];
if (/^data-/.test(attribute.name)) {
type = attribute.name.replace('data-', '');
path = attribute.value.split('.');
context = path.shift();
keypath = path.join('.');
bindings.push(new Rivets.Binding(node, type, contexts[context], keypath));
}
}
}
for (_k = 0, _len2 = bindings.length; _k < _len2; _k++) {
binding = bindings[_k];
binding.bind();
}
return bindings.length;
view = new Rivets.View(el, contexts);
view.bind();
return view;
}
};
......
......@@ -10,7 +10,7 @@ Rivets = {}
class Rivets.Binding
# All information about the binding is passed into the constructor; the DOM
# element, the type of binding, the context object and the keypath at which to
# listed to for changes.
# listen to for changes.
constructor: (@el, @type, @context, @keypath) ->
@routine = Rivets.bindings[@type] || attributeBinding @type
......@@ -30,6 +30,25 @@ class Rivets.Binding
@el.addEventListener 'change', (el) =>
Rivets.config.adapter.publish @context, @keypath, getInputValue el
class Rivets.View
constructor: (@el, @contexts) ->
@build()
build: =>
@bindings = []
for node in @el.getElementsByTagName '*'
for attribute in node.attributes
if /^data-/.test attribute.name
type = attribute.name.replace 'data-', ''
path = attribute.value.split '.'
context = @contexts[path.shift()]
keypath = path.join '.'
@bindings.push new Rivets.Binding node, type, context, keypath
bind: =>
binding.bind() for binding in @bindings
# Returns the current input value for the specified element.
getInputValue = (el) ->
switch el.type
......@@ -83,7 +102,7 @@ Rivets.config =
# The rivets module. This is the public interface that gets exported.
rivets =
# Used to set configuration options and the adapter interface.
# Used to set configuration options.
configure: (options={}) ->
for property, value of options
Rivets.config[property] = value
......@@ -93,21 +112,12 @@ rivets =
register: (routine, routineFunction) ->
Rivets.bindings[routine] = routineFunction
# Binds a set of context objects to the specified DOM element.
# Binds a set of context objects to the specified DOM element. Returns a new
# Rivets.View instance.
bind: (el, contexts = {}) ->
bindings = []
for node in el.getElementsByTagName '*'
for attribute in node.attributes
if /^data-/.test attribute.name
type = attribute.name.replace 'data-', ''
path = attribute.value.split '.'
context = path.shift()
keypath = path.join '.'
bindings.push new Rivets.Binding node, type, contexts[context], keypath
binding.bind() for binding in bindings
bindings.length
view = new Rivets.View(el, contexts)
view.bind()
view
# Exports rivets for both CommonJS and the browser.
if module?
......