Build 0.3.0.
Showing
5 changed files
with
43 additions
and
10 deletions
1 | # Rivets.js | 1 | # Rivets.js |
2 | 2 | ||
3 | 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.4KB minified and gzipped), extensible, and configurable to work with any event-driven model. | 3 | 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. |
4 | 4 | ||
5 | --- | 5 | --- |
6 | 6 | ... | ... |
1 | // rivets.js | 1 | // rivets.js |
2 | // version: 0.2.7 | 2 | // version: 0.3.0 |
3 | // author: Michael Richards | 3 | // author: Michael Richards |
4 | // license: MIT | 4 | // license: MIT |
5 | (function() { | 5 | (function() { |
... | @@ -70,7 +70,8 @@ | ... | @@ -70,7 +70,8 @@ |
70 | }; | 70 | }; |
71 | 71 | ||
72 | Binding.prototype.bind = function() { | 72 | Binding.prototype.bind = function() { |
73 | var _ref; | 73 | var callback, keypath, _i, _len, _ref, _ref1, _ref2, |
74 | _this = this; | ||
74 | if (this.options.bypass) { | 75 | if (this.options.bypass) { |
75 | this.set(this.model[this.keypath]); | 76 | this.set(this.model[this.keypath]); |
76 | } else { | 77 | } else { |
... | @@ -79,7 +80,18 @@ | ... | @@ -79,7 +80,18 @@ |
79 | this.set(Rivets.config.adapter.read(this.model, this.keypath)); | 80 | this.set(Rivets.config.adapter.read(this.model, this.keypath)); |
80 | } | 81 | } |
81 | } | 82 | } |
82 | if (_ref = this.type, __indexOf.call(this.bidirectionals, _ref) >= 0) { | 83 | if ((_ref = this.options.dependencies) != null ? _ref.length : void 0) { |
84 | this.dependencyCallbacks = {}; | ||
85 | _ref1 = this.options.dependencies; | ||
86 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
87 | keypath = _ref1[_i]; | ||
88 | callback = this.dependencyCallbacks[keypath] = function(value) { | ||
89 | return _this.set(_this.options.bypass ? _this.model[_this.keypath] : Rivets.config.adapter.read(_this.model, _this.keypath)); | ||
90 | }; | ||
91 | Rivets.config.adapter.subscribe(this.model, keypath, callback); | ||
92 | } | ||
93 | } | ||
94 | if (_ref2 = this.type, __indexOf.call(this.bidirectionals, _ref2) >= 0) { | ||
83 | return bindEvent(this.el, 'change', this.publish); | 95 | return bindEvent(this.el, 'change', this.publish); |
84 | } | 96 | } |
85 | }; | 97 | }; |
... | @@ -91,9 +103,17 @@ | ... | @@ -91,9 +103,17 @@ |
91 | }; | 103 | }; |
92 | 104 | ||
93 | Binding.prototype.unbind = function() { | 105 | Binding.prototype.unbind = function() { |
94 | var _ref; | 106 | var callback, keypath, _i, _len, _ref, _ref1, _ref2; |
95 | Rivets.config.adapter.unsubscribe(this.model, this.keypath, this.set); | 107 | Rivets.config.adapter.unsubscribe(this.model, this.keypath, this.set); |
96 | if (_ref = this.type, __indexOf.call(this.bidirectionals, _ref) >= 0) { | 108 | if ((_ref = this.options.dependencies) != null ? _ref.length : void 0) { |
109 | _ref1 = this.options.dependencies; | ||
110 | for (_i = 0, _len = _ref1.length; _i < _len; _i++) { | ||
111 | keypath = _ref1[_i]; | ||
112 | callback = this.dependencyCallbacks[keypath]; | ||
113 | Rivets.config.adapter.unsubscribe(this.model, keypath, callback); | ||
114 | } | ||
115 | } | ||
116 | if (_ref2 = this.type, __indexOf.call(this.bidirectionals, _ref2) >= 0) { | ||
97 | return this.el.removeEventListener('change', this.publish); | 117 | return this.el.removeEventListener('change', this.publish); |
98 | } | 118 | } |
99 | }; | 119 | }; |
... | @@ -138,7 +158,7 @@ | ... | @@ -138,7 +158,7 @@ |
138 | bindingRegExp = this.bindingRegExp(); | 158 | bindingRegExp = this.bindingRegExp(); |
139 | eventRegExp = /^on-/; | 159 | eventRegExp = /^on-/; |
140 | parseNode = function(node) { | 160 | parseNode = function(node) { |
141 | var attribute, keypath, model, options, path, pipe, pipes, type, _i, _len, _ref, _results; | 161 | var attribute, context, ctx, dependencies, keypath, model, options, path, pipe, pipes, type, _i, _len, _ref, _results; |
142 | _ref = node.attributes; | 162 | _ref = node.attributes; |
143 | _results = []; | 163 | _results = []; |
144 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | 164 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
... | @@ -156,11 +176,24 @@ | ... | @@ -156,11 +176,24 @@ |
156 | } | 176 | } |
157 | return _results1; | 177 | return _results1; |
158 | })(); | 178 | })(); |
159 | path = pipes.shift().split(/(\.|:)/); | 179 | context = (function() { |
180 | var _j, _len1, _ref1, _results1; | ||
181 | _ref1 = pipes.shift().split('>'); | ||
182 | _results1 = []; | ||
183 | for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { | ||
184 | ctx = _ref1[_j]; | ||
185 | _results1.push(ctx.trim()); | ||
186 | } | ||
187 | return _results1; | ||
188 | })(); | ||
189 | path = context.shift().split(/(\.|:)/); | ||
160 | options.formatters = pipes; | 190 | options.formatters = pipes; |
161 | model = _this.models[path.shift()]; | 191 | model = _this.models[path.shift()]; |
162 | options.bypass = path.shift() === ':'; | 192 | options.bypass = path.shift() === ':'; |
163 | keypath = path.join(); | 193 | keypath = path.join(); |
194 | if (dependencies = context.shift()) { | ||
195 | options.dependencies = dependencies.split(/\s+/); | ||
196 | } | ||
164 | if (eventRegExp.test(type)) { | 197 | if (eventRegExp.test(type)) { |
165 | type = type.replace(eventRegExp, ''); | 198 | type = type.replace(eventRegExp, ''); |
166 | options.special = "event"; | 199 | options.special = "event"; | ... | ... |
This diff is collapsed.
Click to expand it.
1 | { | 1 | { |
2 | "name" : "rivets", | 2 | "name" : "rivets", |
3 | "description" : "Declarative data binding facility.", | 3 | "description" : "Declarative data binding facility.", |
4 | "version" : "0.2.7", | 4 | "version" : "0.3.0", |
5 | "author" : "Michael Richards", | 5 | "author" : "Michael Richards", |
6 | "url" : "http://rivetsjs.com", | 6 | "url" : "http://rivetsjs.com", |
7 | "main" : "./lib/rivets.js", | 7 | "main" : "./lib/rivets.js", | ... | ... |
-
Please register or sign in to post a comment