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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:35:17+00:00 2026-06-13T20:35:17+00:00

Is it wise to put your ajax calls in your Knockout ViewModel or should

  • 0

Is it wise to put your ajax calls in your Knockout ViewModel or should it instead be placed in a Model? I’ve come up with a few approaches but none feel completely right.

Approach 1 – ViewModel Only

window.someDataVM = function() {
   var self = this;

    //used to enable loading indicator
    self.pendingLoad = ko.observable(true);

    self.myData = ko.observableArray();

    self.load = function() {
        //make ajax call and populate myData observable array
    }     
}

Advantages

  • Simplest code structure – easier to maintain

Disadvantages

  • No reuse for data retrieval

Approach 2 – Model and ViewModel With Callback

   window.someDataVM = function() {
       var self = this;

        //used to enable loading indicator
        self.pendingLoad = ko.observable(true);

        self.myData = ko.observableArray();

        self.load = function() {            
            someDataM.load(function(data) {
                //populate myData observable array
            });
        }     
    }

    window.someDataM = function() {
       return {               
          load: function(callback) {
             //get data via ajax and return via callback
          }
       }
    }

Advantages

  • More code reuse on data retrieval (i.e. one place to load someData)

  • Simpler interface that approach 3

Disadvantages

  • Uses callbacks

Approach 3 – Model and ViewModel With Knockout Model

window.someDataVM = function() {
       var self = this;

        //used to enable loading indicator
        self.pendingLoad = ko.observable(true);

        self.myData = ko.observableArray();

        self.load = function() {
            someDataM.load();
        }

        someDataM.isLoaded.subscribe(function(isLoaded) {
            if (isLoaded)  {
               //populate observable array
            }
        });     
}



window.someDataM = function() {
     return {
          isLoaded: ko.observable(false);
          items: [],
          load: function() {
             //get some data, populate items, set isLoaded
          }
     }
    }();

Advantages

  • Doesn’t use callback
  • Keeps data code centralized

Disadvantages

  • Will be complicated to have lots of data entry points (i.e. LoadById, LoadByName, etc, etc.)
  • 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-13T20:35:18+00:00Added an answer on June 13, 2026 at 8:35 pm

    I personally don’t feel comfortable with self-loading VMs. Thus, I would recommend to load data (model) first, and then pass it to the VM.

    Conceptually, it would be something like this:

    function loadData() {
        //load data, can be asynchronously. Then callback
        callback(data);
    }
    
    function callback(data) {
        var vm = new someDataVM(data);
        //do something with VM.
        ko.applyBindings(vm);
    }
    

    This kind of approach makes even more sense when VMs are created by other VMs (multi-screen applications). Also, this approach emphasizes on model-view-viewModel separation by making a chain of logical dependency:

    View => ViewModel => Model
    

    However, VMs can re-load data or make asynchronous calls on user interactions. e.g. user may click a button on the page which loads the current time again. These kinds of interactions will happen inside the existing vm obviously. But the question was related to initial load, which I approach this way.

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

Sidebar

Related Questions

It should be a straight forward and simple answer, but I'm trying to figure
this is kind of a newbie question, sorry but...if a model has no controllers,
Greeting all wise man! I really need your help here as I'm new to
I wanted to know what is better performance-wise: to put the conditions in the
Design wise and performance wise which approach is recommended for handling multiple Zeromq sockets
oh wise list, We need a scalable solution that can handle many concurrent requests
Is it wise to have include guards around template classes? Aren't template classes supposed
Is it wise to use Domain Administrator as a SQL Windows Authentication log-in?
Is there any difference (compiler/interpreter/juju wise, etc) between the two versions of checking the
i'd like to perform element-wise functions on boost matrix and vector types, e.g. take

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.