5b1195cc by Michael Richards

Make adapter part of the configuration instead of an argument for rivets.bind.

1 parent fb5ebc1a
......@@ -10,9 +10,8 @@
Binding.name = 'Binding';
function Binding(el, adapter, type, context, keypath) {
function Binding(el, type, context, keypath) {
this.el = el;
this.adapter = adapter;
this.type = type;
this.context = context;
this.keypath = keypath;
......@@ -27,7 +26,7 @@
if (value == null) {
value = null;
}
return this.routine(this.el, value || this.adapter.read(this.context, this.keypath));
return this.routine(this.el, value || Rivets.config.adapter.read(this.context, this.keypath));
};
Binding.prototype.bind = function() {
......@@ -36,12 +35,12 @@
if (Rivets.config.preloadData) {
this.set();
}
this.adapter.subscribe(this.context, this.keypath, function(value) {
Rivets.config.adapter.subscribe(this.context, this.keypath, function(value) {
return _this.set(value);
});
if (_ref = this.type, __indexOf.call(bidirectionals, _ref) >= 0) {
return this.el.addEventListener('change', function(el) {
return _this.adapter.publish(_this.context, _this.keypath, getInputValue(el));
return Rivets.config.adapter.publish(_this.context, _this.keypath, getInputValue(el));
});
}
};
......@@ -130,7 +129,7 @@
register: function(routine, routineFunction) {
return Rivets.bindings[routine] = routineFunction;
},
bind: function(el, adapter, contexts) {
bind: function(el, contexts) {
var attribute, binding, bindings, context, keypath, node, path, type, _i, _j, _k, _len, _len1, _len2, _ref, _ref1;
if (contexts == null) {
contexts = {};
......@@ -147,7 +146,7 @@
path = attribute.value.split('.');
context = path.shift();
keypath = path.join('.');
bindings.push(new Rivets.Binding(node, adapter, type, contexts[context], keypath));
bindings.push(new Rivets.Binding(node, type, contexts[context], keypath));
}
}
}
......
......@@ -6,24 +6,24 @@
Rivets = {}
class Rivets.Binding
constructor: (@el, @adapter, @type, @context, @keypath) ->
constructor: (@el, @type, @context, @keypath) ->
@routine = Rivets.bindings[@type] || attributeBinding @type
# Sets a value for this binding. Basically just runs the routine on the
# element with a suplied value.
set: (value = null) =>
@routine @el, value || @adapter.read @context, @keypath
@routine @el, value || Rivets.config.adapter.read @context, @keypath
# Subscribes to the context object for changes on the specific keypath.
# Conditionally also does the inverse and listens to the element for changes
# to propogate back to the context object.
bind: =>
@set() if Rivets.config.preloadData
@adapter.subscribe @context, @keypath, (value) => @set value
Rivets.config.adapter.subscribe @context, @keypath, (value) => @set value
if @type in bidirectionals
@el.addEventListener 'change', (el) =>
@adapter.publish @context, @keypath, getInputValue el
Rivets.config.adapter.publish @context, @keypath, getInputValue el
# Returns the current input value for the specified element.
getInputValue = (el) ->
......@@ -86,7 +86,7 @@ rivets =
register: (routine, routineFunction) ->
Rivets.bindings[routine] = routineFunction
bind: (el, adapter, contexts = {}) ->
bind: (el, contexts = {}) ->
bindings = []
for node in el.getElementsByTagName '*'
......@@ -96,7 +96,7 @@ rivets =
path = attribute.value.split '.'
context = path.shift()
keypath = path.join '.'
bindings.push new Rivets.Binding node, adapter, type, contexts[context], keypath
bindings.push new Rivets.Binding node, type, contexts[context], keypath
binding.bind() for binding in bindings
bindings.length
......