Support For Anti-Cache In Zepto’s Ajax Requests

If you have been using Zepto (the ultra light webkit target JS framework) you would have noticed that some of the elements which you have gotten used to with a JS framework, particularly with jQuery, are missing from Zepto.

Zepto’s github page quotes that it is not a goal for Zepto to give 100% match of jQuery. However, there are some important additions that I have made to it in the version that I have for our app. Here’s one,

// Add anti-cache in url if needed

if ( settings.cache === false ) {

var ts = Date.now(), rquery = /\?/, rts = /([?&])_=[^&]*/,

// try replacing _= if it is there

ret = settings.url.replace( rts, “$1_=” + ts );

// if nothing was replaced, add timestamp to the end

settings.url = ret + ( ( ret === settings.url ) ? ( rquery.test( settings.url ) ? “&” : “?” ) + “_=” + ts : “” );

//console.log(“url: ” + settings.url);

}

Add this little snippet to the $.ajax function inside Zepto. To use anti-cache you have to send, you guessed it, “cache: false” as one of the properties in the ajax settings when you call on Zepto to make the async request. I have not reinvented this technique. I looked at the latest jQuery source and made modifications to it to make it work with Zepto. If there is a proven, simpler way to do it be sure let me know. I know that this just works so I used this.

Advertisements