Skip to content

Instantly share code, notes, and snippets.

@devinus
Created April 1, 2025 07:38
Show Gist options
  • Save devinus/71953d91a54788e71771ecc4a6525b4e to your computer and use it in GitHub Desktop.
Save devinus/71953d91a54788e71771ecc4a6525b4e to your computer and use it in GitHub Desktop.
Helpful SproutCore Binding Transforms
SC.Binding.lt = function (max) {
return this.transform(function (value, binding) {
return SC.typeOf(value) === SC.T_NUMBER && value < max;
});
};
SC.Binding.gt = function (min) {
return this.transform(function (value, binding) {
return SC.typeOf(value) === SC.T_NUMBER && value > min;
});
};
SC.Binding._every = function (paths, compareFn) {
var keys = [], gate, idx, len, key, binding, transform;
gate = SC.Object.create();
for (idx = 0, len = paths.length; idx < len; idx++) {
key = 'value'+(idx+1);
keys.push(key);
binding = paths[idx];
if (SC.typeOf(binding) === SC.T_STRING) {
binding = SC.Binding.oneWay(binding);
}
binding.to(key, gate).connect();
}
transform = function () {
var values = [], idx, len;
for (idx = 0, len = keys.length; idx < len; idx++) {
values.push(this.get(keys[idx]));
}
return values.every(compareFn);
};
gate.mixin({
all: SC.Function.property(transform, keys).cacheable()
});
gate.registerDependentKey('all', keys);
return SC.Binding.from('all', gate).oneWay();
};
SC.Binding.all = function () {
return SC.Binding._every(arguments, function (v) { return !!v; });
};
SC.Binding.none = function () {
return SC.Binding._every(arguments, function (v) { return !v; });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment