da922b4e by Michael Richards

Merge pull request #5 from pgib/master

Event listener fix and minor documentation addition
2 parents b1d391a7 4a68a877
...@@ -10,11 +10,16 @@ Rivets.js is alpha software. While it should work well enough for prototyping an ...@@ -10,11 +10,16 @@ Rivets.js is alpha software. While it should work well enough for prototyping an
10 10
11 No contrived example here yet, but the `rivets` module is simple. It exposes three main functions; a `configure` function that is used to set configuration options and the adapter interface, a `register` function that is used to register custom data bindings and a `bind` function that is used to bind a set of context objects to a DOM element. 11 No contrived example here yet, but the `rivets` module is simple. It exposes three main functions; a `configure` function that is used to set configuration options and the adapter interface, a `register` function that is used to register custom data bindings and a `bind` function that is used to bind a set of context objects to a DOM element.
12 12
13 The `bind` function takes two arguements; the parent DOM element that you wish to bind to and a set of context objects. 13 The `bind` function takes two arguments; the parent DOM element that you wish to bind to and a set of context objects.
14 14
15 rivets.bind(el, {user: currentUser, item: item}); 15 rivets.bind(el, {user: currentUser, item: item});
16 16
17 Configuring Rivets.js is required before anything can be binded, as binding is dependant on having an adapter defined. Here's a sample configuration for using Rivets.js with Backbone.js. 17 Context objects are referenced in your data-binding declarations using dot-notation:
18
19 <input type="text" data-value="currentUser.email">
20 <input type="checkbox" data-enabled="item.active">
21
22 Configuring Rivets.js is required before anything can be bound, as binding is dependant on having an adapter defined. Here's a sample configuration for using Rivets.js with Backbone.js.
18 23
19 rivets.configure({ 24 rivets.configure({
20 adapter: { 25 adapter: {
...@@ -22,15 +27,15 @@ Configuring Rivets.js is required before anything can be binded, as binding is d ...@@ -22,15 +27,15 @@ Configuring Rivets.js is required before anything can be binded, as binding is d
22 obj.on('change:' + keypath, function(m, v) { callback(v) }); 27 obj.on('change:' + keypath, function(m, v) { callback(v) });
23 }, 28 },
24 read: function(obj, keypath) { 29 read: function(obj, keypath) {
25 obj.get(keypath); 30 return obj.get(keypath);
26 }, 31 },
27 publish: function(obj, keypath, value) { 32 publish: function(obj, keypath, value) {
28 obj.set(keypath, data); 33 obj.set(keypath, value);
29 } 34 }
30 } 35 }
31 }); 36 });
32 37
33 To add custom data bindings, use the `register` function and pass in an identifier for the binding as well as the binding function. Binding functions take two arguments; `el` which is the DOM element and `value` which is any new incomming value from the observed context object. 38 To add custom data bindings, use the `register` function and pass in an identifier for the binding as well as the binding function. Binding functions take two arguments; `el` which is the DOM element and `value` which is any new incoming value from the observed context object.
34 39
35 rivets.register('active', function(el, value){ 40 rivets.register('active', function(el, value){
36 el.className = value ? 'active' : 'inactive'; 41 el.className = value ? 'active' : 'inactive';
......
1 // Generated by CoffeeScript 1.3.1 1 // Generated by CoffeeScript 1.3.3
2 (function() { 2 (function() {
3 var Rivets, attributeBinding, bidirectionals, getInputValue, rivets, stateBinding, 3 var Rivets, attributeBinding, bidirectionals, getInputValue, rivets, stateBinding,
4 __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, 4 __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
...@@ -8,8 +8,6 @@ ...@@ -8,8 +8,6 @@
8 8
9 Rivets.Binding = (function() { 9 Rivets.Binding = (function() {
10 10
11 Binding.name = 'Binding';
12
13 function Binding(el, type, context, keypath, formatters) { 11 function Binding(el, type, context, keypath, formatters) {
14 this.el = el; 12 this.el = el;
15 this.type = type; 13 this.type = type;
...@@ -48,7 +46,9 @@ ...@@ -48,7 +46,9 @@
48 return _this.set(value); 46 return _this.set(value);
49 }); 47 });
50 if (_ref = this.type, __indexOf.call(bidirectionals, _ref) >= 0) { 48 if (_ref = this.type, __indexOf.call(bidirectionals, _ref) >= 0) {
51 return this.el.addEventListener('change', function(el) { 49 return this.el.addEventListener('change', function(e) {
50 var el;
51 el = e.target || e.srcElement;
52 return Rivets.config.adapter.publish(_this.context, _this.keypath, getInputValue(el)); 52 return Rivets.config.adapter.publish(_this.context, _this.keypath, getInputValue(el));
53 }); 53 });
54 } 54 }
...@@ -60,8 +60,6 @@ ...@@ -60,8 +60,6 @@
60 60
61 Rivets.View = (function() { 61 Rivets.View = (function() {
62 62
63 View.name = 'View';
64
65 function View(el, contexts) { 63 function View(el, contexts) {
66 this.el = el; 64 this.el = el;
67 this.contexts = contexts; 65 this.contexts = contexts;
...@@ -85,8 +83,9 @@ ...@@ -85,8 +83,9 @@
85 }; 83 };
86 84
87 View.prototype.build = function() { 85 View.prototype.build = function() {
88 var attribute, context, dataRegExp, keypath, node, path, pipes, type, _i, _len, _ref, _results; 86 var attribute, bindingRegExp, context, keypath, node, path, pipes, type, _i, _len, _ref, _results;
89 this.bindings = []; 87 this.bindings = [];
88 bindingRegExp = this.bindingRegExp();
90 _ref = this.el.getElementsByTagName('*'); 89 _ref = this.el.getElementsByTagName('*');
91 _results = []; 90 _results = [];
92 for (_i = 0, _len = _ref.length; _i < _len; _i++) { 91 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
...@@ -97,9 +96,8 @@ ...@@ -97,9 +96,8 @@
97 _results1 = []; 96 _results1 = [];
98 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { 97 for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
99 attribute = _ref1[_j]; 98 attribute = _ref1[_j];
100 dataRegExp = new RegExp(this.data, 'g'); 99 if (bindingRegExp.test(attribute.name)) {
101 if (this.bindingRegExp().test(attribute.name)) { 100 type = attribute.name.replace(bindingRegExp, '');
102 type = attribute.name.replace(this.bindingRegExp(), '');
103 pipes = attribute.value.split('|').map(function(pipe) { 101 pipes = attribute.value.split('|').map(function(pipe) {
104 return pipe.trim(); 102 return pipe.trim();
105 }); 103 });
......
...@@ -31,7 +31,8 @@ class Rivets.Binding ...@@ -31,7 +31,8 @@ class Rivets.Binding
31 Rivets.config.adapter.subscribe @context, @keypath, (value) => @set value 31 Rivets.config.adapter.subscribe @context, @keypath, (value) => @set value
32 32
33 if @type in bidirectionals 33 if @type in bidirectionals
34 @el.addEventListener 'change', (el) => 34 @el.addEventListener 'change', (e) =>
35 el = e.target or e.srcElement
35 Rivets.config.adapter.publish @context, @keypath, getInputValue el 36 Rivets.config.adapter.publish @context, @keypath, getInputValue el
36 37
37 # Parses and stores the binding data for an entire view binding. 38 # Parses and stores the binding data for an entire view binding.
...@@ -50,11 +51,12 @@ class Rivets.View ...@@ -50,11 +51,12 @@ class Rivets.View
50 build: => 51 build: =>
51 @bindings = [] 52 @bindings = []
52 53
54 bindingRegExp = @bindingRegExp()
55
53 for node in @el.getElementsByTagName '*' 56 for node in @el.getElementsByTagName '*'
54 for attribute in node.attributes 57 for attribute in node.attributes
55 dataRegExp = new RegExp(@data, 'g') 58 if bindingRegExp.test attribute.name
56 if @bindingRegExp().test attribute.name 59 type = attribute.name.replace bindingRegExp, ''
57 type = attribute.name.replace @bindingRegExp(), ''
58 pipes = attribute.value.split('|').map (pipe) -> pipe.trim() 60 pipes = attribute.value.split('|').map (pipe) -> pipe.trim()
59 path = pipes.shift().split '.' 61 path = pipes.shift().split '.'
60 context = @contexts[path.shift()] 62 context = @contexts[path.shift()]
......