IE not going along very well with bind function

This is actually rather a note to myself.

I am doing some client-side these days. JQuery,datatables,Backbone,Underscore etc..

I was using $.getJSON like below which works perfectly on firefox:

$.getJSON(“/api/sar/?_=” + Math.random() + “&” + query, function(data) {

//do some stuff here

}.bind(this));

However,it didn’t work on IE.Plus, no error message as you can imagine.I did lots of research and just before starting banging my head against walls,i found that there is an incompatibility between IE and jquery bind method. I don’t think it is valid for all cases but that’s why i found out thanks to this entry in stackoverflow.

As a quick solution,JQUERY spec recommends to use $.proxy function when binding this value of the function.So the code makes both browsers happy is:

 $.getJSON(“/api/sar/?_=” + Math.random() + “&” + query,  $.proxy(function(data) {

   // do some stuff here

     },this));

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s