I have a Knockout Viewmodel that uses the mapping plugin. After mapping the JSON object, I create some computed values, like so:
/* viewmodel setup and mapping */
myViewModel
.formattedGiftAmount = ko.computed({
read: function () {
return parseInt(this.giftAmount(), 10).formatMoney();
}
, write: function (value) {
this.giftAmount(value.unformatMoney());
}
, owner: this
})
.formattedGoal = ko.computed({
read: function () {
return parseInt(this.goalAmount(), 10).formatMoney();
}
, write: function (value) {
this.goalAmount(value.unformatMoney());
}
, owner: this
});
Don't worry so much about what the code does, I'm more concerned with the pattern. As you can see, the two properties formattedGiftAmount()
and formattedGoal()
have nearly identical objects that define them. The only difference is what properties they modify. I'm potentially going to have many more instances like this, so I was wondering if there was any way to make this more reusable. I can imagine doing something like this[prop]()
, but I can't quite figure out how to inject that into the object and get it to work.
PS: I've seen this but it still doesn't quite do what I'm looking for.
You can add a function to your view model that creates a formatted property based on an unformatted property:
Then you could call it for your properties: