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 8822637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:09:20+00:00 2026-06-14T06:09:20+00:00

Context: I’m quite new to what I like to think of as, serious javascript/jquery

  • 0

Context: I’m quite new to what I like to think of as, “serious javascript/jquery coding”, my previous attempts would probably be considered treason :P.

My question: I’ve noticed a pattern in some of our client side view models and would like to consolidate some of them into a single .js file.

Everything seems to be working for most scenarios, except for the screens where i’d need to create an additional set of observables, that don’t necessarily map to my JS object returned from the server.

 var AdminPages = AdminPages || {};  

 AdminPages.SimplePageVM: function (options) {

            var self = this;

            self.hasChanges = function () {};        
            self.isValid = function () {};

            // CRUD Actions
            self.get = function () {
                $.ajax({
                   url: options.getUrl,
                   dataType: 'json',
                   data: !$.isEmptyObject(options.someId) ? { someId: options.someId} : null,
                   success: function (result) {            
                       self.observables = ko.mapping.fromJS(result);
                       ko.editable(self.observables);
                       ko.applyBindings(self, $('form')[0]);
                   },
                   error: function (result) {}
                });
            };
            self.save = function () {};
            self.edit = function () {};
            self.cancel = function () {};

            // Initialise the viewmodel on create
            self.get();                      
  }

Id like to add the following to the view model. I’m thinking i need to create an entirely new object, (as self.observables only get created on the success of the get function), and then add my new object and its properties on item bind.

what id like to add:

 self.newObject.IsPercentageEvaluation = 
            ko.computed(function () {
                var isPercentage = self.observables.IsPercentageBased() == 'true';    
                  if (isPercentage) {                        
                      self.observables.BalancePercentage('40');
                  } else {
                      self.observables.BalancePercentage('');
                  }
                return isPercentage;
            });

And to call it all:

$(function () {
        var obj = {
             IsPercentageEvaluation = ko.computed(...);
        };

        AdminPages.SimplePageVM({
            getUrl: '@Url.Action("Get", "SomeController")',
            editUrl: '@Url.Action("Update", "SomeController")',
            organisationId: '@ViewBag.OrganisationID',
            newObject: obj
        });

} ($));

I’d just like to confirm if this is the correct way of approaching this situation? Or if there is a better way, eg making use of a certain java script pattern, or something to that extent?

  • 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-14T06:09:21+00:00Added an answer on June 14, 2026 at 6:09 am

    I worked on a large project last spring and we tried followed a pattern similar to what you are using.

    There are issues that need to be considered-

    • Inside your ajax.success function, we routinely needed ability to modify the returned view mode. For example, add ko.computed’s. I would suggest that you add an overrideable function for this that you call before the ko.applybindings.
    • You need ability to handle situation where your application has a problem returning the data. For example, database is down or a web service is offline. You need to have a graceful way of handling these situations. We did this by extending our view model so that these conditions were returned on separate properties.
    • If you are returning row data to be displayed in table data, you may have to modify your ajax.success function to handle an array of data. We always seemed to run into issues with this.

    I do like your view model. It is a flexible and extensible piece of code. I will be borrowing it on our next project.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Context: single page web applications written in JavaScript and AMD modules (like require.js) What
Context I've just installed postgres 9.1.4 on Lion, and i would like to write
Context Sometimes, I like reading code on paper rather than on screen (so I
Context Clojure Agents are NOT sent new values. They are sent a function which
Context I am trying to understand the internals of a big, hairy JavaScript library.
Context I'm currently reading about Clojure's implementation of monads: org.clojure/algo.monads Intuitively, reduce looks like
Context I have this piece of Java Code btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent
Context: From my javascript web UI, I launch a long-running (several minutes) operation that
context: onSuccess javascript method after an ajax post How do I obtain my id
I am trying to render a haml file in a javascript response like so:

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.