750bf3ef by Michael Richards

Use the Rivets namespace for the Binding class, core bindings and default config…

…. Add a public configure function.
1 parent 8ef7c842
// Generated by CoffeeScript 1.3.1
(function() {
var Rivets,
var Rivets, attributeBinding, bidirectionals, getInputValue, rivets, stateBinding,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
Rivets = {
Helpers: {}
};
Rivets = {};
Rivets.Binding = (function() {
......@@ -22,7 +20,7 @@
this.set = __bind(this.set, this);
this.routine = Rivets.bindings[this.type] || Rivets.Helpers.attributeBinding(this.type);
this.routine = Rivets.bindings[this.type] || attributeBinding(this.type);
}
Binding.prototype.set = function(value) {
......@@ -38,9 +36,9 @@
this.set() && this.adapter.subscribe(this.context, this.keypath, function(value) {
return _this.set(value);
});
if (_ref = this.type, __indexOf.call(Rivets.bidirectionals, _ref) >= 0) {
if (_ref = this.type, __indexOf.call(bidirectionals, _ref) >= 0) {
return this.el.addEventListener('change', function(el) {
return _this.adapter.publish(_this.context, _this.keypath, Rivets.Helpers.getInputValue(el));
return _this.adapter.publish(_this.context, _this.keypath, getInputValue(el));
});
}
};
......@@ -49,7 +47,7 @@
})();
Rivets.Helpers.getInputValue = function(el) {
getInputValue = function(el) {
switch (el.type) {
case 'text':
case 'textarea':
......@@ -62,7 +60,7 @@
}
};
Rivets.Helpers.attributeBinding = function(attr) {
attributeBinding = function(attr) {
return function(el, value) {
if (value) {
return el.setAttribute(attr, value);
......@@ -72,24 +70,26 @@
};
};
Rivets.Helpers.stateBinding = function(attr, inverse) {
stateBinding = function(attr, inverse) {
if (inverse == null) {
inverse = false;
}
return function(el, value) {
var binding;
binding = Rivets.Helpers.attributeBinding(attr);
binding = attributeBinding(attr);
return binding(el, inverse === !value ? attr : false);
};
};
bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected'];
Rivets.bindings = {
checked: Rivets.Helpers.stateBinding('checked'),
selected: Rivets.Helpers.stateBinding('selected'),
disabled: Rivets.Helpers.stateBinding('disabled'),
unchecked: Rivets.Helpers.stateBinding('checked', true),
unselected: Rivets.Helpers.stateBinding('selected', true),
enabled: Rivets.Helpers.stateBinding('disabled', true),
checked: stateBinding('checked'),
selected: stateBinding('selected'),
disabled: stateBinding('disabled'),
unchecked: stateBinding('checked', true),
unselected: stateBinding('selected', true),
enabled: stateBinding('disabled', true),
text: function(el, value) {
return el.innerText = value || '';
},
......@@ -107,9 +107,23 @@
}
};
Rivets.bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected'];
Rivets.config = {
preloadData: true
};
Rivets["interface"] = {
rivets = {
configure: function(options) {
var property, value, _i, _len, _results;
if (options == null) {
options = {};
}
_results = [];
for (value = _i = 0, _len = options.length; _i < _len; value = ++_i) {
property = options[value];
_results.push(Rivets.config[property] = value);
}
return _results;
},
register: function(routine, routineFunction) {
return Rivets.bindings[routine] = routineFunction;
},
......@@ -143,9 +157,9 @@
};
if (typeof module !== "undefined" && module !== null) {
module.exports = Rivets["interface"];
module.exports = rivets;
} else {
this.rivets = Rivets["interface"];
this.rivets = rivets;
}
}).call(this);
......
......@@ -3,12 +3,11 @@
# author : Michael Richards
# license : MIT
Rivets =
Helpers: {}
Rivets = {}
class Rivets.Binding
constructor: (@el, @adapter, @type, @context, @keypath) ->
@routine = Rivets.bindings[@type] || Rivets.Helpers.attributeBinding @type
@routine = Rivets.bindings[@type] || attributeBinding @type
# Sets a value for this binding. Basically just runs the routine on the
# element with a suplied value.
......@@ -21,42 +20,46 @@ class Rivets.Binding
bind: =>
@set() and @adapter.subscribe @context, @keypath, (value) => @set value
if @type in Rivets.bidirectionals
if @type in bidirectionals
@el.addEventListener 'change', (el) =>
@adapter.publish @context, @keypath, Rivets.Helpers.getInputValue el
@adapter.publish @context, @keypath, getInputValue el
# Returns the current input value for the specified element.
Rivets.Helpers.getInputValue = (el) ->
getInputValue = (el) ->
switch el.type
when 'text', 'textarea', 'password', 'select-one' then el.value
when 'checkbox', 'radio' then el.checked
# Returns an attribute binding routine for the specified attribute. This is what
# `registerBinding` falls back to when there is no routine for the binding type.
Rivets.Helpers.attributeBinding = (attr) -> (el, value) ->
attributeBinding = (attr) -> (el, value) ->
if value then el.setAttribute attr, value else el.removeAttribute attr
# Returns a state binding routine for the specified attribute. Can optionally be
# negatively evaluated. This is used to build a lot of the core state binding
# routines.
Rivets.Helpers.stateBinding = (attr, inverse = false) -> (el, value) ->
binding = Rivets.Helpers.attributeBinding(attr)
stateBinding = (attr, inverse = false) -> (el, value) ->
binding = attributeBinding(attr)
binding el, if inverse is !value then attr else false
# Bindings that should also be observed for changes on the DOM element in order
# to propogate those changes back to the model object.
bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected']
# Core binding routines.
Rivets.bindings =
checked:
Rivets.Helpers.stateBinding 'checked'
stateBinding 'checked'
selected:
Rivets.Helpers.stateBinding 'selected'
stateBinding 'selected'
disabled:
Rivets.Helpers.stateBinding 'disabled'
stateBinding 'disabled'
unchecked:
Rivets.Helpers.stateBinding 'checked', true
stateBinding 'checked', true
unselected:
Rivets.Helpers.stateBinding 'selected', true
stateBinding 'selected', true
enabled:
Rivets.Helpers.stateBinding 'disabled', true
stateBinding 'disabled', true
text: (el, value) ->
el.innerText = value or ''
html: (el, value) ->
......@@ -68,13 +71,17 @@ Rivets.bindings =
hide: (el, value) ->
el.style.display = if value then 'none' else ''
# Bindings that should also be observed for changes on the DOM element in order
# to propogate those changes back to the model object.
Rivets.bidirectionals = ['value', 'checked', 'unchecked', 'selected', 'unselected']
# Default configuration.
Rivets.config =
preloadData: true
# The rivets module exposes `register` and `bind` functions to register new
# binding routines and bind contexts to DOM elements.
Rivets.interface =
rivets =
configure: (options={}) ->
for property, value in options
Rivets.config[property] = value
register: (routine, routineFunction) ->
Rivets.bindings[routine] = routineFunction
......@@ -95,6 +102,6 @@ Rivets.interface =
# Exports rivets for both CommonJS and the browser.
if module?
module.exports = Rivets.interface
module.exports = rivets
else
@rivets = Rivets.interface
\ No newline at end of file
@rivets = rivets
\ No newline at end of file
......