67c5564b by Michael Richards

Build 0.3.2.

1 parent 58598d14
# Rivets.js
Rivets.js is a declarative data binding facility that plays well with existing frameworks such as [Backbone.js](http://backbonejs.org), [Spine.js](http://spinejs.com) and [Stapes.js](http://hay.github.com/stapes/). It aims to be lightweight (1.9KB minified and gzipped), extensible, and configurable to work with any event-driven model.
Rivets.js is a declarative data binding facility that plays well with existing frameworks such as [Backbone.js](http://backbonejs.org), [Spine.js](http://spinejs.com) and [Stapes.js](http://hay.github.com/stapes/). It aims to be lightweight (2.3KB minified and gzipped), extensible, and configurable to work with any event-driven model.
---
......
// rivets.js
// version: 0.3.1
// version: 0.3.2
// author: Michael Richards
// license: MIT
(function() {
var Rivets, attributeBinding, bindEvent, eventBinding, getInputValue, rivets, unbindEvent,
var Rivets, attributeBinding, bindEvent, eventBinding, getInputValue, iterationBinding, rivets, unbindEvent,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__slice = [].slice,
__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; };
......@@ -34,11 +34,16 @@
this.formattedValue = __bind(this.formattedValue, this);
if (this.options.special === "event") {
this.routine = eventBinding(this.type);
} else {
this.routine = Rivets.routines[this.type] || attributeBinding(this.type);
this.routine = (function() {
switch (this.options.special) {
case "event":
return eventBinding(this.type);
case "iteration":
return iterationBinding(this.type);
default:
return Rivets.routines[this.type] || attributeBinding(this.type);
}
}).call(this);
this.formatters = this.options.formatters || [];
}
......@@ -61,9 +66,11 @@
if (this.options.special === "event") {
this.routine(this.el, value, this.currentListener);
return this.currentListener = value;
} else if (this.options.special === "iteration") {
return this.routine(this.el, value, this);
} else {
if (value instanceof Function) {
value = value();
value = value.call(this.model);
}
return this.routine(this.el, value);
}
......@@ -104,6 +111,7 @@
Binding.prototype.unbind = function() {
var callback, keypath, _i, _len, _ref, _ref1, _ref2;
if (!this.options.bypass) {
Rivets.config.adapter.unsubscribe(this.model, this.keypath, this.set);
if ((_ref = this.options.dependencies) != null ? _ref.length : void 0) {
_ref1 = this.options.dependencies;
......@@ -116,6 +124,7 @@
if (_ref2 = this.type, __indexOf.call(this.bidirectionals, _ref2) >= 0) {
return this.el.removeEventListener('change', this.publish);
}
}
};
return Binding;
......@@ -152,36 +161,57 @@
};
View.prototype.build = function() {
var bindingRegExp, el, eventRegExp, node, parseNode, _i, _len, _ref, _results,
var bindingRegExp, el, eventRegExp, iterationRegExp, iterator, node, parseNode, skipNodes, _i, _len, _ref, _results,
_this = this;
this.bindings = [];
skipNodes = [];
iterator = null;
bindingRegExp = this.bindingRegExp();
eventRegExp = /^on-/;
iterationRegExp = /^each-/;
parseNode = function(node) {
var attribute, context, ctx, dependencies, keypath, model, options, path, pipe, pipes, splitPath, type, _i, _len, _ref, _results;
var a, attribute, binding, context, ctx, dependencies, keypath, model, n, options, path, pipe, pipes, splitPath, type, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, _results;
if (__indexOf.call(skipNodes, node) < 0) {
_ref = node.attributes;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
attribute = _ref[_i];
if (bindingRegExp.test(attribute.name)) {
type = attribute.name.replace(bindingRegExp, '');
if (iterationRegExp.test(type)) {
if (!_this.models[type.replace(iterationRegExp, '')]) {
_ref1 = node.getElementsByTagName('*');
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
n = _ref1[_j];
skipNodes.push(n);
}
iterator = [attribute];
}
}
}
}
_ref2 = iterator || node.attributes;
_results = [];
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
attribute = _ref2[_k];
if (bindingRegExp.test(attribute.name)) {
options = {};
type = attribute.name.replace(bindingRegExp, '');
pipes = (function() {
var _j, _len1, _ref1, _results1;
_ref1 = attribute.value.split('|');
var _l, _len3, _ref3, _results1;
_ref3 = attribute.value.split('|');
_results1 = [];
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
pipe = _ref1[_j];
for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) {
pipe = _ref3[_l];
_results1.push(pipe.trim());
}
return _results1;
})();
context = (function() {
var _j, _len1, _ref1, _results1;
_ref1 = pipes.shift().split('>');
var _l, _len3, _ref3, _results1;
_ref3 = pipes.shift().split('>');
_results1 = [];
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
ctx = _ref1[_j];
for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) {
ctx = _ref3[_l];
_results1.push(ctx.trim());
}
return _results1;
......@@ -192,6 +222,7 @@
model = _this.models[splitPath.shift()];
options.bypass = path.indexOf(":") !== -1;
keypath = splitPath.join();
if (model) {
if (dependencies = context.shift()) {
options.dependencies = dependencies.split(/\s+/);
}
......@@ -199,12 +230,27 @@
type = type.replace(eventRegExp, '');
options.special = "event";
}
_results.push(_this.bindings.push(new Rivets.Binding(node, type, model, keypath, options)));
if (iterationRegExp.test(type)) {
type = type.replace(iterationRegExp, '');
options.special = "iteration";
}
binding = new Rivets.Binding(node, type, model, keypath, options);
binding.view = _this;
_this.bindings.push(binding);
}
}
if (iterator) {
for (_l = 0, _len3 = iterator.length; _l < _len3; _l++) {
a = iterator[_l];
node.removeAttribute(a.name);
}
_results.push(iterator = null);
} else {
_results.push(void 0);
}
}
return _results;
}
};
_ref = this.els;
_results = [];
......@@ -289,6 +335,44 @@
};
};
iterationBinding = function(name) {
return function(el, collection, binding) {
var data, item, itemEl, iteration, m, n, previous, _i, _j, _len, _len1, _ref, _ref1, _results;
if (binding.iterated != null) {
_ref = binding.iterated;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
iteration = _ref[_i];
iteration.view.unbind();
iteration.el.parentNode.removeChild(iteration.el);
}
} else {
binding.marker = document.createComment(" rivets: each-" + name + " ");
el.parentNode.insertBefore(binding.marker, el);
el.parentNode.removeChild(el);
}
binding.iterated = [];
_results = [];
for (_j = 0, _len1 = collection.length; _j < _len1; _j++) {
item = collection[_j];
data = {};
_ref1 = binding.view.models;
for (n in _ref1) {
m = _ref1[n];
data[n] = m;
}
data[name] = item;
itemEl = el.cloneNode(true);
previous = binding.iterated[binding.iterated.length - 1] || binding.marker;
binding.marker.parentNode.insertBefore(itemEl, previous.nextSibling);
_results.push(binding.iterated.push({
el: itemEl,
view: rivets.bind(itemEl, data)
}));
}
return _results;
};
};
attributeBinding = function(attr) {
return function(el, value) {
if (value) {
......
{
"name" : "rivets",
"description" : "Declarative data binding facility.",
"version" : "0.3.1",
"version" : "0.3.2",
"author" : "Michael Richards",
"url" : "http://rivetsjs.com",
"main" : "./lib/rivets.js",
......
# rivets.js
# version : 0.3.1
# version : 0.3.2
# author : Michael Richards
# license : MIT
......