Clear cache or clear javascript variables in Phonegap app after ajax completes

I have created an Android app using Phonegap, jquery and Kendo UI. Also I have multiple ajax requests.

Now in my application, I have an ajax request to generate an invoice that uses PUT when adding data to my Rest service. Everything works if I have already created a transaction (invoice) and try to create another one that doesn’t go back to 1 and it doesn’t create a new transaction but only returns the last created transaction (NOTE: it calls Successful callback from my ajax) ...

My question is how can I clear the cache on android without leaving the app.

Update

I found that when I successfully create an invoice after this, the variables are still populated with the data stored from the previous transaction. So how can I reset somehow do everything to allow new data. I added this location.reload(true);

one but would like to find another solution to this problem. thank.

+3


source to share


1 answer


I ran into a very similar issue while testing the ToDo backbone.js sample application in Developing a Basic .js Addy Osmani Application using Cordova (PhoneGap) and running it on an Android 2.3 emulator.

The problem I ran into was that the first GETs or PUTs were made for a specific url, but subsequent requests or updates to the same model instance (i.e. the same url) were not sent to the RESTful server though jQuery.ajax calls returned success.

The problem turned out to be an issue with the Android browser in Android 2.3 not passing subsequent requests to the same URL. After confirming here: http://jorgenmodin.net/index_html/jquery-and-android-2.3-calling-a-url-again-gives-the-cached-result I ended up modifying the backbone model and url set: functions to add a unique timestamp to the URL. This made each URL unique, and then the browser made an ajax request each time as originally needed.

Here's the piece of code I used for the collection:

var TodoList = Backbone.Collection.extend({

   // Reference to this collection model.
   model: app.Todo,

   urlRoot: 'http://<RESTful_server_address_goes_here>/todos/',

   url: function() {
       var base = _.result(this, 'urlRoot') || urlError();
       var ts = new Date().getTime();
       // Make sure that there is a trailing slash on the url, and add a timestamp
       return base + (base.charAt(base.length - 1) === '/' ? '' : '/') + '?_' + ts;
   },

   ...
});

      



And here is an excerpt from the code for the model:

  app.Todo = Backbone.Model.extend({

      url: function() {
          var base = _.result(this.collection, 'urlRoot') || urlError();
          // if not creating a new item, then append the item id
          if (!this.isNew()) base = base + 
                               (base.charAt(base.length - 1) === '/' ? '' : '/') + 
                               encodeURIComponent(this.id);
          // now append a timestamp
          var ts = new Date().getTime();
          return base + '?_' + ts;
      },

      ...
  });

      

Note that among the many things I tried before coming up with this solution, it was just setting the jQuery.ajax cache (see jQuery.ajax documentation) to false in the override for the .sync backbone, but that DOESN'T WORK. Here's the code anyway:

app.sync = function(method, model, options) {

          // THIS DOES NOT FIX THE PROBLEM !!
          // IT ONLY PREVENTS CACHING ON GET OPERATIONS 
          // HOWEVER PUT OPERATIONS ARE STILL CACHED BY ANDROID 2.3 BROWSER!!
          options.cache = false;

      return Backbone.sync(method, model, options);  

};

      

+1


source







All Articles