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

  • Home
  • SEARCH
  • 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 7838877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:14:41+00:00 2026-06-02T15:14:41+00:00

Im trying to develop a class in JavaScript I can use to access a

  • 0

Im trying to develop a class in JavaScript I can use to access a load of data that is gathered by an AJAX request easily. The only problem is I need to make the members of the class accessible only once the AJAX call is complete. Ideally what I would like to end up is something where by I can call this in a script:

courses.getCourse('xyz').complete = function () {
    // do something with the code
}

And this will only fire after the AJAX call has been complete and the data structures in the “class” are ready to be used. Ideally I dont want to have to create a .complete member for every function in the class

Here is the “class” I am trying to make so far:

var model_courses = (function() {

    var cls = function () {

        var _storage = {}; // Used for storing course related info
        _storage.courses = {}; // Used for accessing courses directly
        _storage.references = new Array(); // Stores all available course IDs

        var _ready = 0;
        $.ajax({
            type: "GET",
            url: "data/courses.xml",
            dataType: "xml",
            success: function(xml) {
                $(xml).find("course").each(function() {

                    _storage.courses[$(this).attr('id')] = {
                        title       : $(this).find('title').text(),
                        description : $(this).find('description').text(),
                        points      : $(this).find('points').text()
                    }
                    _storage.references.push($(this).attr('id'))

                })

            }
        })

        console.log(_storage.courses)

    }
    cls.prototype = {
            getCourse: function (courseID) {
                console.log(cls._storage)
            },
            getCourses: function () {
                return _storage.courses
            },
            getReferences: function (),
                return _storage.references
            }

    }
    return cls
})()

At the moment getCourse will be fired before the AJAX request is complete and obviously it will have no data to access.

Any ideas will be greatly appreciated, im stuck on this one!

  • 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-02T15:14:43+00:00Added an answer on June 2, 2026 at 3:14 pm

    The following changes allow you to make the AJAX request just once and you can call your function like

    courses.getCourse('xyz', function(course){
        // Use course here
    });
    

    Here are the changes

    var model_courses = (function() {
    
        // This is what gets returned by the $.ajax call
        var xhr;
        var _storage = {}; // Used for storing course related info
        _storage.courses = {}; // Used for accessing courses directly
        _storage.references = []; // Stores all available course IDs
    
        var cls = function () {
            xhr = $.ajax({
                type: "GET",
                url: "data/courses.xml",
                dataType: "xml",
                success: function(xml) {
                    $(xml).find("course").each(function() {
    
                        _storage.courses[$(this).attr('id')] = {
                            title       : $(this).find('title').text(),
                            description : $(this).find('description').text(),
                            points      : $(this).find('points').text()
                        }
                        _storage.references.push($(this).attr('id'))
    
                    });
                }
            });
        }
        cls.prototype = {
                // Made changes here, you'd have to make the same 
                // changes to getCourses and getReferences
                getCourse: function (courseID, callback) {
                    if (xhr.readyState == 4) {
                         callback(_storage.courses[courseID]);
                    }
                    else {
                       xhr.done(function(){
                          callback(_storage.courses[courseID]);
                       })
                    }
    
                },
                getCourses: function () {
                    return _storage.courses
                },
                getReferences: function (),
                    return _storage.references
                }
    
        }
        return cls
    })()
    

    As a side note, your module pattern will not work very well if you need to instantiate two of these model_courses objects, since the storage objects are all shared in your self calling function’s closure. You usually don’t mix the module pattern with prototypes (returning a constructor from a module), unless you really know what you are doing, that is, the shared closure variables work as static properties of your class.

    This is what I would do if I were you (since you really want private variables)

    function ModelCourses() {
        var storage = {
          courses: {},
          references: []
        };
    
        var xhr = $.ajax({
            type: "GET",
            url: "data/courses.xml",
            dataType: "xml",
            success: function(xml) {
                $(xml).find("course").each(function() {   
                    storage.courses[$(this).attr('id')] = {
                        title       : $(this).find('title').text(),
                        description : $(this).find('description').text(),
                        points      : $(this).find('points').text()
                    }
                    storage.references.push($(this).attr('id'))
                })    
            }
        });
    
        this.getCourse = function(courseId, callback) {
            function getCourse() {
                callback(storage.courses[courseID])
            }
            if (xhr.readyState == 4) {
                getCourse();
            }
            else {
                xhr.done(getCourse);
            }
        };   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to develop a class that supports an asynchronus method invocation. This is
I am trying to develop everything in sharepoint as features so I can easily
I'm trying to develop a basic GPA Calculator. I have an AllCourses class that
I'm trying to develop a JQuery plug-in that will access an outside XML feed
I am trying to develop a class that allows me to run a socket
I have been trying to develop a CustomButton class that extends MovieClip, however I
I've been trying to develop a dynamic library in C++ that can be run-time
I'm trying to develop a Java web based application that can work flow different
I'm trying to develop a pretty simple (for now) wrapper class around int ,
I am trying to develop an algorithm wherein I have a Location Class. In

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.