Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7999083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:23:58+00:00 2026-06-04T15:23:58+00:00

I have a Knockout Viewmodel that uses the mapping plugin. After mapping the JSON

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:24:00+00:00Added an answer on June 4, 2026 at 3:24 pm

    You can add a function to your view model that creates a formatted property based on an unformatted property:

    myViewModel.addFormattedProperty = function(formattedName, unformattedName) {
        this[formattedName] = ko.computed({
            read: function() {
                return parseInt(this[unformattedName](), 10).formatMoney();                        
            },
            write: function(value) {
                this[unformattedName](value.unformatMoney());
    
            },
            owner: this
        });
    };
    

    Then you could call it for your properties:

    myViewModel.addFormattedProperty('formattedGiftAmount', 'giftAmount');
    myViewModel.addFormattedProperty('formattedGoalAmount', 'goalAmount');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a complex page that uses knockout to render the contents via templates.
I have a script that initializes knockout by applying an empty viewmodel to my
I have a piece of code working in knockout.js like this: <div> ... some
I have a Knockout.js view model that looks like this: UserName:null, FirstName:null, LastName:null, Countries:{
I have the following table template that is rendered through knockout: <table class=gv data-bind=visible:
I have a page that requires jQuery.tmpl, but I want to use native knockout
I feel like this must have been asked before, but I've looked at Knockout
I have problem, my Knockout ViewModel is NO function and this IS NEEDED: vmNeu
We are using knockout and the knockout mapping plugin to facilitate databinding in our
Lets say I have a viewmodel like this var viewModel = { Dtos: [{

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.