Currently I am working on sort of a JavaScript function library for all things SharePoint to pass around to some of the SP developers at my work. The idea is to just take some simple features I see being repeated enough and stuffing the Ajax call into a single well documented function for ease of use. The problem is, this stuff is ALL new to me and my boss doesn't know that. <:( Here is an example of one I wrote up from my "ListHelper.js".
function getValueByID(field, refID)
{
if(setReady())
{
var ajaxCall = $.getJSON(SiteURL+"_vti_bin/listdata.svc/"+currentList);
ajaxCall.success(function (jsonData)
{
var data = jsonData.d.results;
for(i = 0; i < data.length; i++)
{
if(data[i].Id == refID)
{
console.log(data[i][field]);
return data[i][field];
break;
}
}
});
ajaxCall.error(function () { alert("GetValueByTitle Failed"); });
}
}
setReady() just returns a boolean based on if SiteURL and currentList have been set properly.
Anyway the logic here works fine. (As in I can see the proper value in console) But it takes like a second to process and its a pain because I haven't been able to make JavaScript WAIT until it actually returns a value before continuing on after the function call. So if i try to call this function in a separate .html file I often get 'undefined' by the time i need to use it. So I have been trying to think of ways to speed it up. (It seems like there's GOTTA be a faster way to do what I'm trying to do anyways) Here's what I came up with by poking around on the web.
//TODO figure out why this filtering system isn't working!
function getValueByID(field, refID)
{
if(setReady())
{
var url = SiteURL+"_vti_bin/listdata.svc/"+currentList+"?filter=Id eq "+refID;
console.log(url);
var ajaxCall = $.getJSON(url);
ajaxCall.success(function (jsonData)
{
var data = jsonData.d.results
console.log(data[0][field]);
return data[0][field];
});
ajaxCall.error(function () { alert("GetValueByTitle Failed"); });
}
}
In this case the filtering doesn't work at all. It always just returns the first item in the list like it would if i removed the "?filter=Id eq #" all together. Any ideas on why this filtering thing isn't working? Or even.. if I am approaching this wrong, maybe a point in the right direction of what i SHOULD be doing? Any help is greatly appreciated. Thanks!