baconjs / bacon.jquery

MIT License
74 stars 23 forks source link

Using other types than strings as bacon.jquery model values #34

Closed jliuhtonen closed 10 years ago

jliuhtonen commented 10 years ago

Bacon.jquery does not currently support using other types than strings with the models it creates. Trying to set model value to integer for example does not reflect to the UI, which is understandable as the values are strings in the DOM after all.

Anyway, it would be nice to use e.g. numbers with your Bacon.$.radioButtonGroup and hide the conversions from application logic. For example:

var initialId = 23256;
var radioElems = $("#myForm").find("input:radio")
var radioGroupWithIdValues = Bacon.$.radioGroupValue(radioElems, initialId)
...
radioGroupWithIdValues.set(54347)

One can work around this by making a bidirectional conversion between types with Bacon.Model's Bacon.Binding, like in this fiddle: http://jsfiddle.net/jliuhtonen/EfmLU/

Or perhaps make some changes to bacon.jquery directly...

Do you think that it would be useful to have something like this in Bacon.Jquery or Bacon.Model?

jliuhtonen commented 10 years ago

After thinking about this for a while I guess I'll just go with Bacon.Binding. Changing the way radioGroupValue checks for value equality on calling set would be a bad idea. Now equality is checked with CoffeeScript's is. JS type coercion is kinda nasty and IMO should be avoided anyway. A method for creating conversions in Bacon.Model would create little to none extra value. If this a common case, then maybe a Bacon.$.intRadioGroupValue (or something) could be a nice utility.

raimohanska commented 10 years ago

I've run into the same issue myself (integer values). PR with full solution will be accepted :)