c3d676e0 by Michael Richards

Merge branch 'ie-polyfills-and-fixes'

2 parents 4f048110 b4993914
......@@ -6,6 +6,12 @@
Rivets = {};
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, "");
};
}
Rivets.Binding = (function() {
function Binding(el, type, model, keypath, formatters) {
......@@ -40,7 +46,11 @@
this.set(Rivets.config.adapter.read(this.model, this.keypath));
}
if (_ref = this.type, __indexOf.call(bidirectionals, _ref) >= 0) {
return this.el.addEventListener('change', this.publish);
if (window.addEventListener) {
return this.el.addEventListener('change', this.publish);
} else {
return this.el.attachEvent('change', this.publish);
}
}
};
......@@ -79,7 +89,7 @@
};
View.prototype.build = function() {
var attribute, bindingRegExp, keypath, model, node, path, pipes, type, _i, _len, _ref, _results;
var attribute, bindingRegExp, keypath, model, node, path, pipe, pipes, type, _i, _len, _ref, _results;
this.bindings = [];
bindingRegExp = this.bindingRegExp();
_ref = this.el.getElementsByTagName('*');
......@@ -94,9 +104,16 @@
attribute = _ref1[_j];
if (bindingRegExp.test(attribute.name)) {
type = attribute.name.replace(bindingRegExp, '');
pipes = attribute.value.split('|').map(function(pipe) {
return pipe.trim();
});
pipes = (function() {
var _k, _len2, _ref2, _results2;
_ref2 = attribute.value.split('|');
_results2 = [];
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
pipe = _ref2[_k];
_results2.push(pipe.trim());
}
return _results2;
})();
path = pipes.shift().split('.');
model = this.models[path.shift()];
keypath = path.join('.');
......
......@@ -6,6 +6,9 @@
# The Rivets namespace.
Rivets = {}
# Polyfill For String::trim.
unless String::trim then String::trim = -> @replace /^\s+|\s+$/g, ""
# A single binding between a model attribute and a DOM element.
class Rivets.Binding
# All information about the binding is passed into the constructor; the DOM
......@@ -32,7 +35,12 @@ class Rivets.Binding
@set Rivets.config.adapter.read @model, @keypath
if @type in bidirectionals
@el.addEventListener 'change', @publish
# Check to see if addEventListener is available.
if window.addEventListener
@el.addEventListener 'change', @publish
else
# Assume we are in IE and use attachEvent.
@el.attachEvent 'change', @publish
# Publishes the value currently set on the input element back to the model.
publish: (e) =>
......@@ -60,7 +68,7 @@ class Rivets.View
for attribute in node.attributes
if bindingRegExp.test attribute.name
type = attribute.name.replace bindingRegExp, ''
pipes = attribute.value.split('|').map (pipe) -> pipe.trim()
pipes = (pipe.trim() for pipe in attribute.value.split '|')
path = pipes.shift().split '.'
model = @models[path.shift()]
keypath = path.join '.'
......