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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:34:55+00:00 2026-05-11T13:34:55+00:00

I come from the land of Java, C#, etc. I am working on a

  • 0

I come from the land of Java, C#, etc. I am working on a javascript report engine for a web application I have. I am using jQuery, AJAX, etc. I am having difficulty making things work the way I feel they should – for instance, I have gone to what seems like too much trouble to make sure that when I make an AJAX call, my callback has access to the object’s members. Those callback functions don’t need to be that complicated, do they? I know I must be doing something wrong. Please point out what I could be doing better – let me know if the provided snippet is too much/too little/too terrible to look at.

What I’m trying to do:

  • On page load, I have a select full of users.
  • I create the reports (1 for now) and add them to a select box.
  • When both a user and report are selected, I run the report.
  • The report involves making a series of calls – getting practice serieses, leagues, and tournaments – for each league and tournament, it gets all of those serieses, and then for each series it grabs all games.
  • It maintains a counter of the calls that are active, and when they have all completed the report is run and displayed to the user.

Code:

//Initializes the handlers and reports function loadUI() {     loadReports();     $('#userSelect').change(updateRunButton);     $('#runReport').click(runReport);     updateRunButton();     return;     $('#userSelect').change(loadUserGames);     var user = $('#userSelect').val();     if(user) {         getUserGames(user);     } }  //Creates reports and adds them to the select function loadReports() {     var reportSelect = $('#reportSelect');     var report = new SpareReport();     engine.reports[report.name] = report;     reportSelect.append($('<option/>').text(report.name));      reportSelect.change(updateRunButton); }  //The class that represents the 1 report we can run right now. function SpareReport() {     this.name = 'Spare Percentages';     this.activate = function() {      };      this.canRun = function() {         return true;     };      //Collects the data for the report.  Initializes/resets the class variables,     //and initiates calls to retrieve all user practices, leagues, and tournaments.     this.run = function() {         var rC = $('#rC');         var user = engine.currentUser();         rC.html('<img src='/img/loading.gif' alt='Loading...'/> <span id='reportProgress'>Loading games...</span>');         this.pendingOperations = 3;         this.games = [];         $('#runReport').enabled = false;         $.ajaxSetup({'error':(function(report) {             return function(event, XMLHttpRequest, ajaxOptions, thrownError) {                 report.ajaxError(event, XMLHttpRequest, ajaxOptions, thrownError);             };         })(this)});          $.getJSON('/api/leagues', {'user':user}, (function(report) {             return function(leagues) {                 report.addSeriesGroup(leagues);             };         })(this));         $.getJSON('/api/tournaments', {'user':user}, (function(report) {             return function(tournaments) {                 report.addSeriesGroup(tournaments);             };         })(this));         $.getJSON('/api/practices', {'user':user}, (function(report) {             return function(practices) {                 report.addSerieses(practices);             };         })(this));     };      // Retrieves the serieses (group of IDs) for a series group, such as a league or     // tournament.     this.addSeriesGroup = function(seriesGroups) {         var report = this;         if(seriesGroups) {             $.each(seriesGroups, function(index, seriesGroup) {                 report.pendingOperations += 1;                 $.getJSON('/api/seriesgroup', {'group':seriesGroup.key}, (function(report) {                     return function(serieses) {                         report.addSerieses(serieses);                     };                 })(report));             });         }         this.pendingOperations -= 1;         this.tryFinishReport();     };      // Retrieves the actual serieses for a series group.  Takes a set of     // series IDs and retrieves each series.     this.addSerieses = function(serieses) {         var report = this;         if(serieses) {             $.each(serieses, function(index, series) {                 report.pendingOperations += 1;                 $.getJSON('/api/series', {'series':series.key}, (function(report) {                     return function(series) {                         report.addSeries(series);                     };                 })(report));             });         }         this.pendingOperations -= 1;         this.tryFinishReport();     };      // Adds the games for the series to the list of games     this.addSeries = function(series) {         var report = this;         if(series && series.games) {             $.each(series.games, function(index, game) {                 report.games.push(game);             });         }         this.pendingOperations -= 1;         this.tryFinishReport();     };      // Checks to see if all pending requests have completed - if so, runs the     // report.     this.tryFinishReport = function() {         if(this.pendingOperations > 0) {             return;         }         var progress = $('#reportProgress');         progress.text('Performing calculations...');         setTimeout((function(report) {             return function() {                 report.finishReport();             };         })(this), 1);     }      // Performs report calculations and displays them to the user.     this.finishReport = function() {         var rC = $('#rC');          //snip a page of calculations/table generation         rC.html(html);          $('#rC table').addClass('tablesorter').attr('cellspacing', '1').tablesorter({'sortList':[[3,1]]});     };      // Handles errors (by ignoring them)     this.ajaxError = function(event, XMLHttpRequest, ajaxOptions, thrownError) {         this.pendingOperations -= 1;     };      return true; }  // A class to track the state of the various controls.  The 'series set' stuff // is for future functionality. function ReportingEngine() {     this.seriesSet = [];     this.reports = {};     this.getSeriesSet = function() {         return this.seriesSet;     };     this.clearSeriesSet = function() {         this.seriesSet = [];     };     this.addGame = function(series) {         this.seriesSet.push(series);     };     this.currentUser = function() {         return $('#userSelect').val();     };     this.currentReport = function() {         reportName = $('#reportSelect').val();         if(reportName) {             return this.reports[reportName];         }         return null;     }; }  // Sets the enablement of the run button based on the selections to the inputs function updateRunButton() {     var report = engine.currentReport();     var user = engine.currentUser();     setRunButtonEnablement(report != null && user != null); }  function setRunButtonEnablement(enabled) {     if(enabled) {         $('#runReport').removeAttr('disabled');     } else {         $('#runReport').attr('disabled', 'disabled');     }  }  var engine = new ReportingEngine();  $(document).ready( function() {     loadUI(); });  function runReport() {     var report = engine.currentReport();     if(report == null) {         updateRunButton();         return;     }     report.run(); } 

I am about to start adding new reports, some of which will operate on only a subset of user’s games. I am going to be trying to use subclasses (prototype?), but if I can’t figure out how to simplify some of this… I don’t know how to finish that sentence. Help!

  • 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. 2026-05-11T13:34:55+00:00Added an answer on May 11, 2026 at 1:34 pm
    $.getJSON('/api/leagues', {'user':user}, (function(report) {         return function(leagues) {             report.addSeriesGroup(leagues);         };     })(this)); 

    Can be written as:

    var self = this; $.getJSON('/api/leagues', {'user':user}, (function(leagues) {             self.addSeriesGroup(leagues);         }); 

    The function-returning-function is more useful when you’re inside a loop and want to bind to a variable that changes each time around the loop.

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

Sidebar

Related Questions

I come from a strong Java background and in recent years have been also
I come from heavy web application dev background, and having some problems representing my
I come from a Java background, where packages are used, not namespaces. I'm used
I come from a CVS background. I'm currently investigating using SVN for a project.
I come from the Java world, where you can hide variables and functions and
I come from classes object orientation languages and recently I have been learning those
I come from more of a Java background. In the last year or two,
I come from a Java background and with any servlets-based technology, it's trivial to
Bear with me, I'm new to NUnit. I come from the land of Rails,
I come from the land of OpenGL, so I'm similar with glColor functions, and

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.