Here's a helper function to make it more convenient to wrap them up. It's a bit rough, but does the job.
/**
* For use when you really want to intercept two-way Angular bindings.
* (And usually there's another better way)
*
* Example
* <input
* ng-model="asProp(getCheckbox, setCheckbox, someArg1, someArg2).value"
* type="checkbox" />
* ...
* $scope.asProp = lib.asProp;
* $scope.getCheckbox = function(someArg1, someArg2) { return ... };
* $scope.setCheckbox = function(newValue, someArg1, someArg2) { ... };
*
* Takes a getter and a setter function.
* And optionally additional arguments.
* Returns an object with a single property called 'value'.
* 'value' uses the provided getter and setter functions.
* Additional arguments are passed to the getter and setter:
* getter(additionalArgs) -> currentValue
* setter(newValue, additionalArgs)
*/
lib.asProp = function (getter, setter /* .. args */) {
var res = {};
var args = _.toArray(arguments).slice(2);
Object.defineProperty(res, 'value', {
get: function () {
return getter.apply(null, args);
},
set: function (val) {
var args2 = [val].concat(args);
setter.apply(null, args2);
},
enumerable: true,
configurable: true
});
return res;
};
1 comment:
I’d actually love to be half 솔카지노 of|part of} neighborhood where I can get feedback from other educated people that share the same curiosity. I actually have} learn this put up and if I could I need to recommend you some attention-grabbing issues or tips. I appreciate you finding the effort and time to place this informative article together.
Post a Comment