Big javascript post

All of this javascript goodness became useful while I was using jquery to call Sharepoint web services, specifically after I wrapped all of the functionality into a class.

1. ┬áContext – this issue arose while using jquery’s .ajax() method as so:

$.ajax({ url: "../_vti_bin/lists.asmx", type: "POST", dataType: "xml", data: soapEnv, complete: this.processResult, contentType: "text/xml; charset="utf-8"" });

I kept getting an error saying it couldn’t find the processResult() function. Turns out the “this” keyword wasn’t returning the context of the class. Instead, it was returning the context of the ajax() function call. After searching the intrawebs, I came accross this article. So, by adding the code and changing “this.processResult” to “$.context(this).callback(‘processResult’)” the problem was solved.

1A. Context and Recursion – this issue occurred while using a recursive function to write out a┬ámenu with child elements. Once again, this worked fine until I put everything into a class. However, once I did that, I started getting some weird issues with my for loop. The counter would increment twice while the recursive call was made. I wasn’t able to figure this one out, so I just unrolled the function.

2. Array sorting (mostly for my benefit) – a cool way to sort an xml array in descending order:

this.sortRows = function(a, b){ return ($(a).attr("columnName") - $(b).attr("columnName")) }

Then call the function like this:

array.sort(this.sortRows);