Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit a8c387d39d00eb07fae6af20f686d95eddb32d45 @mikeric committed Apr 16, 2012
Showing with 267 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +20 −0 LICENSE
  3. +64 −0 README.md
  4. +101 −0 lib/rivets.js
  5. +18 −0 package.json
  6. +63 −0 src/rivets.coffee
@@ -0,0 +1 @@
+.DS_Store
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Michael Richards
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
+# Rivets.js
+
+Rivets.js is a declarative, observer-based DOM-binding facility that plays well with existing frameworks and supports multiple contexts. It aims to be lightweight, extensible, and configurable to work with any event-driven model.
+
+## Disclaimer
+
+Rivets.js is alpha software. While it should work well enough for prototyping and weekend projects, it is still undergoing major development. APIs are subject to change.
+
+## Usage
+
+No contrived example here yet, but the `rivets` module is simple. It exposes a single `bind` function that takes three arguements; the parent DOM element that you wish to bind to, an adapter interface, and a set of context objects.
+
+ rivets.bind(el, adapter, {user: currentUser, item: item});
+
+#### Available bindings:
+
+- **data-text**: one-way binding that sets the node's text.
+- **data-value**: two-way binding that sets the node's value.
+- **data-show**: one-way binding that sets the node's display state.
+- **data-hide**: one-way inverse binding that sets the node's display state.
+- **data-enabled**: one-way binding that sets the node's enabled state.
+- **data-disabled**: one-way inverse binding that sets the node's enabled state.
+- **data-checked**: two-way binding that sets the node's checked state.
+- **data-unchecked**: two-way inverse binding that sets the node's checked state.
+- **data-selected**: two-way binding that sets the node's selected state.
+- **data-unselected**: two-way inverse binding that sets the node's checked state.
+- **data-[attribute]**: one-way binding that sets the node's attribute value (currently only for a few select attributes).
+
+## Adapters
+
+Rivets.js is model interface-agnostic, meaning that it can work with any event-driven model by way of defining an adapter. The following standard adapter observes `change:[attribute]` style events and works with frameworks like [Backbone.js](http://documentcloud.github.com/backbone/), [Spine.js](http://spinejs.com/) and [Stapes.js](http://hay.github.com/stapes/) with minimal alterations for each. If your model's events API differs, it's trivial to write an adapter that will work with your own model objects.
+
+An adapter is an object that responds to `subscribe`, `read` and `publish`.
+
+ backboneAdapter = {
+ subscribe: function(obj, keypath, callback) {
+ obj.on('change:' + keypath, function(m, v) { callback(v) });
+ },
+
+ read: function(obj, keypath) {
+ obj.get(keypath);
+ },
+
+ publish: function(obj, keypath, value) {
+ obj.set(keypath, data);
+ }
+ }
+
+#### subscribe(obj, keypath, callback)
+
+- **obj**: The model object that we want to subscribe to for attribute changes. These are what get passed in as context objects when calling `rivets.bind`.
+- **keypath**: The attribute name that we want to scope to when subscribing to the model's attribute changes. This would most commonly be a single key or a nested keypath (support for nested keypaths depends on if your model object publishes events on nested attribute changes). You may define and intercept this value in any format, for example if your model object uses `author[profile][bio]` instead of `author.profile.bio`.
+- **callback**: The function that performs the binding routine. Call this function with the new attribute value.
+
+#### read(obj, keypath)
+
+- **obj**: The model object that we want to read the attribute from.
+- **keypath**: The attribute name that we want to read from the model object.
+
+#### publish(obj, keypath, value)
+
+- **obj**: The model object that we want to set the new attribute value on.
+- **keypath**: The attribute name that we want to set on the model object.
+- **value**: The new attribute value that we want to set on the model.
@@ -0,0 +1,101 @@
+
+ window.rivets = (function() {
+ var attr, bindableAttributes, bindings, getInputValue, registerBinding, setAttribute, _fn, _i, _len;
+ registerBinding = function(el, interface, contexts, type, callback) {
+ return $("*[data-" + type + "]", el).each(function() {
+ var context, inputValue, keypath, path;
+ var _this = this;
+ path = $(this).attr("data-" + type).split('.');
+ context = path.shift();
+ keypath = path.join('.');
+ callback(this, interface.read(contexts[context], keypath));
+ interface.subscribe(contexts[context], keypath, function(value) {
+ return callback(_this, value);
+ });
+ if (inputValue = getInputValue(this)) {
+ return interface.publish(contexts[context], keypath, inputValue);
+ }
+ });
+ };
+ setAttribute = function(el, attr, value, mirrored) {
+ if (mirrored == null) mirrored = false;
+ if (value) {
+ return $(el).attr(attr, mirrored ? attr : value);
+ } else {
+ return $(el).removeAttr(attr);
+ }
+ };
+ getInputValue = function(el) {
+ switch ($(el).attr('type')) {
+ case 'text':
+ case 'textarea':
+ case 'password':
+ case 'select-one':
+ return $(this).val();
+ case 'checkbox':
+ return $(this).is(':checked');
+ }
+ };
+ bindings = {
+ show: function(el, value) {
+ if (value) {
+ return $(el).show();
+ } else {
+ return $(el).hide();
+ }
+ },
+ hide: function(el, value) {
+ if (value) {
+ return $(el).hide();
+ } else {
+ return $(el).show();
+ }
+ },
+ enabled: function(el, value) {
+ return setAttribute(el, 'disabled', !value, true);
+ },
+ disabled: function(el, value) {
+ return setAttribute(el, 'disabled', value, true);
+ },
+ checked: function(el, value) {
+ return setAttribute(el, 'checked', value, true);
+ },
+ unchecked: function(el, value) {
+ return setAttribute(el, 'checked', !value, true);
+ },
+ selected: function(el, value) {
+ return setAttribute(el, 'selected', value, true);
+ },
+ unselected: function(el, value) {
+ return setAttribute(el, 'checked', !value, true);
+ },
+ text: function(el, value) {
+ return $(el).text(value || '');
+ },
+ value: function(el, value) {
+ return $(el).val(value);
+ }
+ };
+ bindableAttributes = ['id', 'class', 'name', 'src', 'href', 'alt', 'title', 'placeholder'];
+ _fn = function(attr) {
+ return bindings[attr] = function(el, value) {
+ return setAttribute(el, attr, value);
+ };
+ };
+ for (_i = 0, _len = bindableAttributes.length; _i < _len; _i++) {
+ attr = bindableAttributes[_i];
+ _fn(attr);
+ }
+ return {
+ bind: function(el, interface, contexts) {
+ var callback, type, _results;
+ if (contexts == null) contexts = {};
+ _results = [];
+ for (type in bindings) {
+ callback = bindings[type];
+ _results.push(registerBinding(el, interface, contexts, type, callback));
+ }
+ return _results;
+ }
+ };
+ })();
@@ -0,0 +1,18 @@
+{
+ "name" : "rivets",
+ "description" : "Declarative DOM-binding facility.",
+ "version" : "0.1.0",
+ "author" : "Michael Richards",
+ "main" : "./lib/rivets.js",
+ "licenses" : [{
+ "type" : "MIT",
+ "url" : "https://github.com/mikeric/rivets/blob/master/LICENSE"
+ }],
+ "repository" : {
+ "type" : "git",
+ "url" : "https://github.com/mikeric/rivets.git"
+ },
+ "dependencies" : {
+ "jquery" : "1.5.0"
+ }
+}
@@ -0,0 +1,63 @@
+# rivets.js
+# version : 0.1.0
+# author : Michael Richards
+# license : MIT
+
+window.rivets = do ->
+ registerBinding = (el, interface, contexts, type, callback) ->
+ $("*[data-#{type}]", el).each ->
+ path = $(this).attr("data-#{type}").split '.'
+ context = path.shift()
+ keypath = path.join '.'
+
+ callback this, interface.read contexts[context], keypath
+
+ interface.subscribe contexts[context], keypath, (value) =>
+ callback this, value
+
+ if inputValue = getInputValue this
+ interface.publish contexts[context], keypath, inputValue
+
+ setAttribute = (el, attr, value, mirrored=false) ->
+ if value
+ $(el).attr attr, if mirrored then attr else value
+ else
+ $(el).removeAttr attr
+
+ getInputValue = (el) ->
+ switch $(el).attr 'type'
+ when 'text', 'textarea', 'password', 'select-one' then $(this).val()
+ when 'checkbox' then $(this).is ':checked'
+
+ bindings =
+ show: (el, value) ->
+ if value then $(el).show() else $(el).hide()
+ hide: (el, value) ->
+ if value then $(el).hide() else $(el).show()
+ enabled: (el, value) ->
+ setAttribute el, 'disabled', !value, true
+ disabled: (el, value) ->
+ setAttribute el, 'disabled', value, true
+ checked: (el, value) ->
+ setAttribute el, 'checked', value, true
+ unchecked: (el, value) ->
+ setAttribute el, 'checked', !value, true
+ selected: (el, value) ->
+ setAttribute el, 'selected', value, true
+ unselected: (el, value) ->
+ setAttribute el, 'checked', !value, true
+ text: (el, value) ->
+ $(el).text value or ''
+ value: (el, value) ->
+ $(el).val value
+
+ bindableAttributes = ['id', 'class', 'name', 'src', 'href', 'alt', 'title', 'placeholder']
+
+ for attr in bindableAttributes
+ do (attr) ->
+ bindings[attr] = (el, value) ->
+ setAttribute el, attr, value
+
+ bind: (el, interface, contexts={}) ->
+ for type, callback of bindings
+ registerBinding el, interface, contexts, type, callback

0 comments on commit a8c387d

Please sign in to comment.