0

NOTE: question edited for clarity 4/5/2014

I am making a method call using JSON RPC that requests data from the server. When that data comes back it fires a callback function. JSON RPC callback functions are automatically supplied output and exception arguments from the server. Currently I have a single function that both makes the RPC request and processes and displays the data. I want to separate my request concerns from my display concerns so that the code is more reusable. In order to do this I need to pass the resultRegion id argument to the callback function as well.

Here's an example of the currently functional but conjoined code:

function customerTable(inputField, resultRegion) {
  var displayAccountTable = function(accounts, exception) {
    if(exception) {
      alert(exception.msg);
    }
    else {
      var headings = ["Account ID", "First Name", "Last Name", "Balance"];
      var rows = new Array();
      for(var i=0; i<accounts.length; i++) {
        var account = accounts[i];
        rows[i] = [account.accountID, account.firstName, account.lastName, account.prettyAccountBalance];
      }
      var table = getTable(headings, rows);
      htmlInsert(resultRegion, table);
    }
  };
  var inputs = commaStringToArray(getRawValue(inputField));
  rpcClient.rpcTester.getAccounts(displayAccountTable, inputs);
}

EDIT: Here's the working code that I landed on after using David SkyMesh's answer.

function customerTable(inputField, resultRegion) {
  var inputs = commaStringToArray(getRawValue(inputField));
  rpcClient.rpcTester.getAccounts(displayAccountTable(resultRegion), inputs);
}

function displayAccountTable(resultRegion) {
  return function (accounts, exception) {
    if(exception) { return alert(exception.msg); }
    var headings = ["Account ID", "First Name", "Last Name", "Balance"];
    var account, rows = [];
    for(var i=0; i<accounts.length; i++) {
      account = accounts[i];
      rows[i] = [account.accountID, account.firstName, account.lastName, account.prettyAccountBalance];
    }
    var table = getTable(headings, rows);
    htmlInsert(resultRegion, table);
  };
}
sage88
  • 4,104
  • 4
  • 31
  • 41
  • Your question is super-confusing. My guess is that you want to seperate the display logic from the request logic. I've provided a pretty simple closure-based answer to seperating the concerns. Is that what you mean? If not, which variables/expressions exactly are you trying to factor out? – David-SkyMesh Apr 05 '14 at 07:43
  • Yeah that's exactly what I wanted to do. Sorry that it's a bit confusing. I'm going to give you answer a test and see whether or not it works for me. If it does I'll edit the question a bit so it's more useful for future users. – sage88 Apr 05 '14 at 14:39

1 Answers1

1

Let's start with the obvious closure-based approach:

function mk_displayAccountTable(inputField, resultRegion) {
  return function (accounts, exception) {
    if (exeption) return alert(exception.msg);
    var headings = ["Account ID", "First Name", "Last Name", "Balance"];
    var i, account, rows = [];
    for(i=0; i<accounts.length; i++) {
      account = accounts[i];
      rows.push([account.accountID, account.firstName, 
                account.lastName, account.prettyAccountBalance]);
    }
    var table = getTable(headings, rows);
    htmlInsert(resultRegion, table);
  };
}

function customerTable(inputField, resultRegion) {
  var inputs = commaStringToArray(getRawValue(inputField));
  rpcClient.rpcTester.getAccounts(
    mk_displayAccountTable(inputField, resultRegion), 
    inputs);
}
David-SkyMesh
  • 5,041
  • 1
  • 31
  • 38
  • This is exactly what I wanted to do, thank you. I altered it a little bit and posted my final code as an edit to the question above. – sage88 Apr 05 '14 at 15:16