When Emulate my app to android have an error cannot call setOptions method of angular cache

280 Views Asked by At

When emulate my app to an android emulator the CordovaLog give me an Error Cannot Call method setOptions of undefined.In my Chrome Browser i have not problem but when i emulate it the result is not good ,do not load all my view but a part of it and do not work.Can anyone tell me how can i define the setOptions or why is undefined

(function () {

'use strict';
angular.module('EventList').factory('EventApi', [ '$http', '$q', '$ionicLoading', 'DSCacheFactory', EventApi]);

function EventApi($http, $q, $ionicLoading, DSCacheFactory)
{
    var AllEventCache = DSCacheFactory.get("AllEventDataCache");


  AllEventCache.setOptions({
        onExpire: function (key, value) {
            getAllEvents()
            .then(function () {

                console.log("Automatically refreshed");

            }, function () {
                console.log("Error putting Expired data");

                AllEventCache.put(key, value);
            });
        }

 });


})();

.run(function ($ionicPlatform, DSCacheFactory) {
    $ionicPlatform.ready(function() {
   // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
      if(window.cordova && window.cordova.plugins.Keyboard) {
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
     }
     if(window.StatusBar) {
     // org.apache.cordova.statusbar required
     StatusBar.styleDefault();
     }

        var AllEventCache = DSCacheFactory("AllEventDataCache", { storageMode:        "localStorage", maxAge: 360000, deleteOnExpire: "aggressive" });

    });
   })
1

There are 1 best solutions below

0
Hugh Hou On

I think I have the solution. I am having the same issue in iOs (it works on Localhost but when build in xcode, it does not work...so pretty much same as your android situation). If you refresh in Chrome like coupled time, you will see the same error as well.

The issue is, you need to put your

DSCacheFactory("AllEventDataCache", { storageMode:        "localStorage", maxAge: 360000, deleteOnExpire: "aggressive" });

into your factory. Instead of defining your AllEventCache in run (in your app.js file I assumed?), take it out and defining directly in your factory js file like this:

angular.module('EventList').factory('EventApi', [ '$http', '$q', '$ionicLoading', 'DSCacheFactory', EventApi]);

function EventApi($http, $q, $ionicLoading, DSCacheFactory)
{

DSCacheFactory("AllEventDataCache", { storageMode:        "localStorage", maxAge: 360000, deleteOnExpire: "aggressive" });
var AllEventCache = DSCacheFactory.get("AllEventDataCache");


AllEventCache.setOptions({
    onExpire: function (key, value) {
        getAllEvents()
        .then(function () {

Hope this work for you as well :)