SC.Binding custom #859

j-Anderson opened this Issue Oct 29, 2012 · 3 comments


None yet

3 participants


I have seen some problems with custom binding.

In the documentation, there is this example :

SC.Binding.notLessThan = function(minValue) {
  return this.transform(function(value, binding) {
    return ((SC.typeOf(value) === SC.T_NUMBER) && (value < minValue)) ? minValue : value ;
  }) ;
} ;

If you test this example with a textFieldView binded on a model with SC.Record.attr(Number) with a notLessThan(10) custom binding.
You will see a textField with '10' value after type '109' then delete the '0' for having '19' and then remove the first '1'. --> The textfield will change the value to '10' ok well done, but if you get the model property MyApp.model.get('myProperty') == 9, why ?

Can it be fixed ? otherwise the documentation will be rectified

I have similar problem with all other custom binding we wrote :

SC.Binding.allowedChars = function(allowedChars) {
    return this.transform(function(value, binding) {
        return (value) ? value.replace(new RegExp("[^" + allowedChars + "]", 'gi'), '') : value;
SproutCore member

I've just updated my pull request #851 to include a fix for your issue.
I'd love to heard your feeback.


Thanks for considering.

I can't test your changes by now.

@nicolasbadia nicolasbadia added a commit to nicolasbadia/sproutcore that referenced this issue Feb 8, 2013
@nicolasbadia nicolasbadia Binding synchronisation fix
Fix an issue with binding where the transformed value was not sync back
if the transform function has changed it.

Bug reported here: #859

This fix can lead existing apps to an infinit loops if the transformed
value is not meant to be sync back with the "from" object and if the
developer forgot to use oneWay instead of from to define the
binding. For this, I've add a developer warning telling him use oneWay.

I've also add the syncTransformedValue parameter to the transform
method. For complex case, where we use isForward to transform the
property differently between the two binded objects, it can be usefull
to disable the synchronisation.
SproutCore member

It's possible that there's a design problem at root here, but this behavior is explicit in the SC.Binding code: transformed values are only applied to the To side; the raw value stays as set on the From side.

Part of the problem here is that you're using view layer bindings to do data layer validation. Per the documentation, transforms are intended to make sure that the To object's values fit its own internal needs.

I'll put some thought into this and see if I can clarify the documentation.

@dcporter dcporter closed this Feb 15, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment