Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>$cacheFactory can help you cache the response. Try to implement the "JsonFactory" this way:</p> <pre><code>app.factory("JsonFactory",function($resource,$cacheFactory){ $cacheFactory("JsonFactory"); var url="myurl?domain=:tabUrl"; return{ getResponse:function(tabUrl){ var retObj=$resource(url,{},{list:{method:"GET",cache:true}}); var response=cache.get(tabUrl); //if response is not cached if(!response){ //send GET request to fetch response response=retObj.list({tabUrl:tabUrl}); //add response to cache cache.put(tabUrl,response); } return cache.get(tabUrl); } }; }); </code></pre> <p>And use this service in controller:</p> <pre><code>app.controller("myCtrl",function($scope,$location,JsonFactory){ $scope.clickCount=0; $scope.jsonpTest = function(){ $scope.result = JsonFactory.getResponse("myTab"); $scope.clickCount++; } }); </code></pre> <p>HTML:</p> <pre><code>&lt;script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"&gt;&lt;/script&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular-resource.js"&gt;&lt;/script&gt; &lt;script src="js/ngResource.js"&gt;&lt;/script&gt; &lt;body ng-app="app"&gt; &lt;div ng-controller="myCtrl"&gt; &lt;div&gt;Clicked: {{clickCount}}&lt;/div&gt; &lt;div&gt;Response: {{result}}&lt;/div&gt; &lt;input type="button" ng-click="jsonpTest()" value="JSONP"/&gt; &lt;/div&gt; &lt;/body&gt; </code></pre> <p>Screenshot:</p> <p><img src="https://i.stack.imgur.com/3eZ44.png" alt="enter image description here"></p> <p><strong>[EDIT] for html5 localStorage solution</strong></p> <p><strong><a href="http://jsbin.com/ORIpUfA/1/edit" rel="nofollow noreferrer">JSBin Demo</a></strong></p> <pre><code>.factory("JsonFactory",function($resource){ var url="ur/URL/:tabUrl"; var liveTime=60*1000; //1 min var response = ""; return{ getResponse:function(tabUrl){ var retObj=$resource(url,{},{list:{method:"GET",cache:true}}); if(('localStorage' in window) &amp;&amp; window.localStorage !== null){ //no cached data if(!localStorage[tabUrl] || new Date().getTime()&gt;localStorage[tabUrl+"_expires"]) { console.log("no cache"); //send GET request to fetch response response=retObj.list({tabUrl:tabUrl}); //add response to cache localStorage[tabUrl] = response; localStorage[tabUrl+"_expires"] = new Date().getTime()+liveTime; } //console.log(localStorage.tabUrl.expires+"..."+new Date().getTime()); return localStorage[tabUrl]; } //client doesn't support local cache, send request to fetch response response=retObj.list({tabUrl:tabUrl}); return response; } }; }); </code></pre> <p>Hope this is helpful for you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload