3a89d3a2 by Michael Richards

Add a class toggle binding, data-class-[class].

1 parent 67c5564b
...@@ -17,6 +17,7 @@ class Rivets.Binding ...@@ -17,6 +17,7 @@ class Rivets.Binding
17 constructor: (@el, @type, @model, @keypath, @options = {}) -> 17 constructor: (@el, @type, @model, @keypath, @options = {}) ->
18 @routine = switch @options.special 18 @routine = switch @options.special
19 when "event" then eventBinding @type 19 when "event" then eventBinding @type
20 when "class" then classBinding @type
20 when "iteration" then iterationBinding @type 21 when "iteration" then iterationBinding @type
21 else Rivets.routines[@type] || attributeBinding @type 22 else Rivets.routines[@type] || attributeBinding @type
22 23
...@@ -118,6 +119,7 @@ class Rivets.View ...@@ -118,6 +119,7 @@ class Rivets.View
118 iterator = null 119 iterator = null
119 bindingRegExp = @bindingRegExp() 120 bindingRegExp = @bindingRegExp()
120 eventRegExp = /^on-/ 121 eventRegExp = /^on-/
122 classRegExp = /^class-/
121 iterationRegExp = /^each-/ 123 iterationRegExp = /^each-/
122 124
123 parseNode = (node) => 125 parseNode = (node) =>
...@@ -153,6 +155,10 @@ class Rivets.View ...@@ -153,6 +155,10 @@ class Rivets.View
153 type = type.replace eventRegExp, '' 155 type = type.replace eventRegExp, ''
154 options.special = "event" 156 options.special = "event"
155 157
158 if classRegExp.test type
159 type = type.replace classRegExp, ''
160 options.special = "class"
161
156 if iterationRegExp.test type 162 if iterationRegExp.test type
157 type = type.replace iterationRegExp, '' 163 type = type.replace iterationRegExp, ''
158 options.special = "iteration" 164 options.special = "iteration"
...@@ -208,6 +214,17 @@ eventBinding = (event) -> (el, bind, unbind) -> ...@@ -208,6 +214,17 @@ eventBinding = (event) -> (el, bind, unbind) ->
208 bindEvent el, event, bind if bind 214 bindEvent el, event, bind if bind
209 unbindEvent el, event, unbind if unbind 215 unbindEvent el, event, unbind if unbind
210 216
217 # Returns a class binding routine for the specified class name.
218 classBinding = (name) -> (el, value) ->
219 elClass = " #{el.className} "
220 hasClass = elClass.indexOf(" #{name} ") != -1
221
222 if !value is hasClass
223 el.className = if value
224 "#{el.className} #{name}"
225 else
226 elClass.replace(" #{name} ", " ").trim()
227
211 # Returns an iteration binding routine for the specified collection. 228 # Returns an iteration binding routine for the specified collection.
212 iterationBinding = (name) -> (el, collection, binding) -> 229 iterationBinding = (name) -> (el, collection, binding) ->
213 if binding.iterated? 230 if binding.iterated?
......