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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:44:59+00:00 2026-06-12T06:44:59+00:00

I have common module on my app that retrieves data from remote server and

  • 0

I have common module on my app that retrieves data from remote server and have get methods to export the retrieved data to other modules.

Lets assume that this module’s name is MyData:

define([], function(){

    return function(opts) {
        var data = null;

        // go get data with ajax
        var getData = function(){
            $.ajax("getData").done(function(response){
                data = response;
            });
        };

        getData();

        // return public api
        if (arguments.length !== 0) {
            var method = arguments[0];
            switch (method) {
                case "get": return data;
            }
        };

    };

});

The problem is when I define other modules that use MyData, everytime it would create a new instance of MyData, causing it to retrieve the data from the server each time.

I could use web storage (or other global variable) to store the returned data, but it seems like a bad solution.

Is there any way to force RequireJS to create only once instance?

P.S:
In my project I user KnockoutJS, and the data variable is an ko.observable, so maybe knockout has a solution for my situation?

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

    RequireJS already loads each module once. You just need to restructure your code a bit:

    // myModule.js
    define(function() {
        // The dataFetch Promise will be initialized when this module
        // is loaded for the first time.
        var dataFetch = $.ajax("getData");
    
        return {
            getData: function () {
                // Return the Promise interface and let callers use the
                // .done, .fail etc. methods on it
                return dataFetch;
            }
        };
    });
    

    Usage:

    define([ "myModule" ], function (myModule) {
        myModule.getData().done(function (data) {
            // use the data
        });
    });
    

    By using the Promise interface returned by $.ajax, you can ensure asynch access to data that’s fetched only once.

    BTW, perhaps the pattern that you’re trying to implement is a Model.

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

Sidebar

Related Questions

I have common functionality that I need to access from all screens of my
So I have a common module which contains processing functions for numbers and types
I have a common comms library that i have written to communicate with our
I have a common problem that I never know how to solve with CSS:
I am distributing an app that uses the Python/C API. I have all standard
In my app, I have dir structure close to the following: src/ api/ server.erl
I have limited Oracle exp. I wrote app for both SQL Server and Oracle,
I've got an application that needs to have multiple app domains to isolate potentially
I have a common silverlight project. This project project, among other things, includes constants
My web application have a chat app module built on SignalR hub. The app

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.