dd452d26 by Michael Richards

Implement the initial data-each-[item] binding.

1 parent 44468934
...@@ -15,10 +15,10 @@ class Rivets.Binding ...@@ -15,10 +15,10 @@ class Rivets.Binding
15 # element, the type of binding, the model object and the keypath at which 15 # element, the type of binding, the model object and the keypath at which
16 # to listen for changes. 16 # to listen for changes.
17 constructor: (@el, @type, @model, @keypath, @options = {}) -> 17 constructor: (@el, @type, @model, @keypath, @options = {}) ->
18 if @options.special is "event" 18 @routine = switch @options.special
19 @routine = eventBinding @type 19 when "event" then eventBinding @type
20 else 20 when "iteration" then iterationBinding @type
21 @routine = Rivets.routines[@type] || attributeBinding @type 21 else Rivets.routines[@type] || attributeBinding @type
22 22
23 @formatters = @options.formatters || [] 23 @formatters = @options.formatters || []
24 24
...@@ -47,6 +47,9 @@ class Rivets.Binding ...@@ -47,6 +47,9 @@ class Rivets.Binding
47 if @options.special is "event" 47 if @options.special is "event"
48 @routine @el, value, @currentListener 48 @routine @el, value, @currentListener
49 @currentListener = value 49 @currentListener = value
50 else if @options.special is "iteration"
51 @iterated or= []
52 @routine @el, value, @
50 else 53 else
51 value = value() if value instanceof Function 54 value = value() if value instanceof Function
52 @routine @el, value 55 @routine @el, value
...@@ -113,6 +116,7 @@ class Rivets.View ...@@ -113,6 +116,7 @@ class Rivets.View
113 @bindings = [] 116 @bindings = []
114 bindingRegExp = @bindingRegExp() 117 bindingRegExp = @bindingRegExp()
115 eventRegExp = /^on-/ 118 eventRegExp = /^on-/
119 iterationRegExp = /^each-/
116 120
117 parseNode = (node) => 121 parseNode = (node) =>
118 for attribute in node.attributes 122 for attribute in node.attributes
...@@ -129,6 +133,7 @@ class Rivets.View ...@@ -129,6 +133,7 @@ class Rivets.View
129 options.bypass = path.indexOf(":") != -1 133 options.bypass = path.indexOf(":") != -1
130 keypath = splitPath.join() 134 keypath = splitPath.join()
131 135
136 if model
132 if dependencies = context.shift() 137 if dependencies = context.shift()
133 options.dependencies = dependencies.split /\s+/ 138 options.dependencies = dependencies.split /\s+/
134 139
...@@ -136,6 +141,10 @@ class Rivets.View ...@@ -136,6 +141,10 @@ class Rivets.View
136 type = type.replace eventRegExp, '' 141 type = type.replace eventRegExp, ''
137 options.special = "event" 142 options.special = "event"
138 143
144 if iterationRegExp.test type
145 type = type.replace iterationRegExp, ''
146 options.special = "iteration"
147
139 @bindings.push new Rivets.Binding node, type, model, keypath, options 148 @bindings.push new Rivets.Binding node, type, model, keypath, options
140 149
141 for el in @els 150 for el in @els
...@@ -180,6 +189,25 @@ eventBinding = (event) -> (el, bind, unbind) -> ...@@ -180,6 +189,25 @@ eventBinding = (event) -> (el, bind, unbind) ->
180 bindEvent el, event, bind if bind 189 bindEvent el, event, bind if bind
181 unbindEvent el, event, unbind if unbind 190 unbindEvent el, event, unbind if unbind
182 191
192 # Returns an iteration binding routine for the specified collection.
193 iterationBinding = (name) -> (el, collection, binding) ->
194 for iteration in binding.iterated
195 iteration.view.unbind()
196 iteration.el.parentNode.removeChild iteration.el
197
198 binding.iterated = []
199
200 for item in collection
201 data = {}
202 data[name] = item
203
204 itemEl = el.cloneNode true
205 el.parentNode.insertBefore itemEl, el
206
207 binding.iterated.push
208 el: itemEl
209 view: rivets.bind itemEl, data
210
183 # Returns an attribute binding routine for the specified attribute. This is what 211 # Returns an attribute binding routine for the specified attribute. This is what
184 # is used when there are no matching routines for an identifier. 212 # is used when there are no matching routines for an identifier.
185 attributeBinding = (attr) -> (el, value) -> 213 attributeBinding = (attr) -> (el, value) ->
......