Browse files

Moved bind.js to it's own repo

  • Loading branch information...
1 parent 7af4672 commit 6c5533fbc03a79cad6ce2ddb20dc8a580dd1f7b6 @remy committed Jun 8, 2013
Showing with 1 addition and 127 deletions.
  1. +1 −27 README.markdown
  2. +0 −100 bind.js
View
28 README.markdown
@@ -25,33 +25,7 @@ ev(links).on('remy', function (event) {
# bind.js / Data Binding
-Simple data to HTML binding (that also supports simple callbacks)
-
-## Example
-
-```javascript
-var data = Bind({
- me: {
- name: '@rem',
- score: 11
- },
- them: {
- score: 0,
- name: '@julieanne'
- }
-}, {
- 'me.score': '#score',
- 'me.name': '.username',
- 'them.score': function (score) {
- alert(data.them.name + ' now has a score of ' + score);
- }
-});
-
-// updating data object updates the HTML, as the second arg is a data mapping
-data.me.score++; // updates #score element
-
-data.them.score++; // shows an alert with "@julieanne now has a score of 1"
-```
+bind.js used to live here, but I've moved it to it's own repo: [https://github.com/remy/bind](https://github.com/remy/bind).
# xhr.js / XHR
View
100 bind.js
@@ -1,100 +0,0 @@
-/*
-
-Simple data to HTML binding.
-
-Requires:
-
-setters/gettings, bind, qSA, getOwnPropertyNames, forEach - basically a decent browser
-
-Arguments:
-
-- obj: this is your data structure that is copied in to the Bind
-- mapping: this is a key/value pair of object path to DOM selector
-
-Example:
-
-var data = Bind({
- me: {
- name: "@rem",
- score: 11
- }
-}, {
- 'me.score': '#score',
- 'me.name': '#name'
-});
-
-Restrictions:
-
-Only supports simple changes to the object - if you change the structure, then the bindings are lost.
-
-i.e. this won't work:
-
-var data = Bind({...}, {...});
-
-data.me.name = '@leftlogic'; //works
-data.me = { // fails
- name: '@rem'
-}
-
-*/
-var Bind = (function () {
-"use strict"; // damn jshint & sublime
-
-var $ = document.querySelectorAll.bind(document),
- forEach = [].forEach;
-
-function extend(target, object, mapping, _path) {
- if (!_path) { _path = []; }
- Object.getOwnPropertyNames(object).forEach(function (key) {
- var value = object[key];
- var path = [].slice.call(_path);
- path.push(key);
-
- var elements;
-
- if (typeof value === "object" && value !== null && !Array.isArray(value)) {
- target[key] = extend(target[key] || {}, value, mapping, path);
- } else {
- var callback;
- if (typeof mapping[path.join('.')] === 'function') {
- callback = mapping[path.join('.')];
- } else {
- callback = function (value) {
- if (callback.elements) {
- forEach.call(callback.elements, function (element) {
- element.innerHTML = value;
- });
- }
- };
- callback.elements = $(mapping[path.join('.')] || '');
- }
-
- Object.defineProperty(target, key, {
- set: function (v) {
- value = v;
- if (callback) {
- callback(value);
- }
- },
- get: function () {
- return value;
- }
- });
-
- target[key] = value;
- }
- });
- return target;
-}
-
-var Bind = function (obj, mapping) {
- if (!this || this === window) {
- return new Bind(obj, mapping);
- }
- extend(this, obj, mapping);
- return this;
-};
-
-return Bind;
-
-})();

0 comments on commit 6c5533f

Please sign in to comment.