Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am struggling with javascript and learning callbacks, and applying this to SharePoint 2010 specific js/jquery methods of getting and savings values from lists. So I am recently getting into the following method (i know there are others but I would like to learn this one):

function getListData()
{
    var gridURL = "http://spdev:42977/sites/devcenter/_vti_bin/ListData.svc/Projects";
    var request = new Sys.Net.WebRequest();
    request.set_httpVerb("GET");
    request.set_url(gridURL);
    request.get_headers()["Accept"] = "application/json";
    request.add_completed(onCompletedCallback);
    request.invoke();
}

function onCompletedCallback(response, eventArgs) {
      var getProject = eval("(" + response.get_responseData() + ")");
      var buildMarkUp = "";
      for (var i = 0; i < getProject.d.results.length; i++)  {
         buildMarkUp += "<div>" + getProject.d.results[i].Title + " </div>";
         var passID = getProject.d.results[i].ProjectID;

         // need a nested call to go out and get items from related lists using the  passID varible, and build table/grid   
      }

so i trying to build a data grid, and for parent item, or project ID/Title that comes back on the list has several related data values stored in other lists that I need to pull, but I can't seem to figure out how to apply callbacks to the above method (over and over for each list I need to pull from since they have their own very specific pre-defined callbacks returning the data already).

So I asked a similiar question on stack which got me pointed in the direction of callbacks in the first place (new to me). I am beginning to understand this in a basic sense....

hence the example here.

I tried playing around with the SP method like:

request.add_completed(onCompletedCallback, passedAdditionalValue);

thinking I might be able to keep passing and build the string through all the repeat functions and callbacks. adding additional parameter to the successfulCallback doesn't work because it breaks the response, eventArgs......response not define.

how would i go about using the getListData() listdata.svc with callback method per above, then use it again (2 or 3 times more) to get additional relative data, using more callbacks? or something like this to achieve the desired result. I can't seem to figure out how to apply the general callbacks to the SP specific example.

greatly appreciated as always!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Cool that you use the built-in asp.net ajax class Sys.Net.WebRequest(). I have always used other ajax frameworks like jQuery ajax, angular $http, amplify request.

But it doesn't matter what you use. Callbacks are vital for ajax implementations. Because you have a series of ajax requests, in order to reuse your functions, you have to pass a callback into your function as parameter. Consider this:

var projectsUrl = "http://spdev:42977/sites/devcenter/_vti_bin/ListData.svc/Projects";
var someOtherUrl = "http://spdev:42977/sites/devcenter/_vti_bin/ListData.svc/OtherStuff";
function getListData(gridUrl, callback)
{
    var request = new Sys.Net.WebRequest();
    request.set_httpVerb("GET");
    request.set_url(gridURL);
    request.get_headers()["Accept"] = "application/json";
    request.add_completed(callback);
    request.invoke();
}

function onProjectsCallback(response, eventArgs) {
      //show your content as you wish

      // nested invokation
      // want to modify "someOtherUrl" based on projects you got, go ahead! 
      getListData(someOtherUrl , callback)   
}

function onOtherCallback(response, eventArgs) {
      // do what you need whith this "other" data
      //nested invokation again, go ahead!
}


//Now the most important. Fire the chain of requests:
getListData(projectsUrl, onProjectsCallback)
share|improve this answer
1  
...thanks so much for this example. this is starting to make much more sense to me now that I can see it; and not just how to make this work for my SP use case, but this concept in general. appreciated as always! –  Justin Feb 2 '13 at 16:32
    
Glad to help. Cool to see that you embrace the client side! –  Anatoly Mironov Feb 2 '13 at 22:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.