javascript - Working with OfflineJS and AngularJS -
i working on angular app want integrate offlinejs functionality. have created general service getting , posting data to/from api,and specific service each module. here code
app.service('methodprovider', function ($http) { var self = this; self.get = function (url) { var obj = { url: url, method: 'get', async: true, headers: { 'content-type': 'application/json' } }; return $http(obj); }; self.post = function (url, data) { var obj = { url: url, method: 'post', async: true, data: json.stringify(data), headers: { 'content-type': 'application/json' } }; return $http(obj); }; self.put = function (url, data) { var obj = { url: url, method: 'put', async: true, headers: { 'content-type': 'application/json' } }; if (typeof data != 'undefined' && data != null) { obj.data = json.stringify(data); } return $http(obj); }; self.delete = function (url) { var obj = { url: url, method: 'post', async: true, headers: { 'content-type': 'application/json' } }; return $http(obj); }; return self; });
and specific module service user module
app.service('usersrvc', function (methodprovider) { var self = this; self.create = function (data) { var url = apiurl + '/user/add'; return methodprovider.post(url, data); }; return self; });
how integrate offlinejs in code , want intercept http request when network connectivity down , resume requests when network connectivity . have studied example unable integrate in angular need example started.
hope helps future users: offlinejs adds offline
object global window object.
offlinejs not trigger check() (it once if checkonload
option set true).
may call offline.check()
before make ajax request server , check connection.
can polling, if app xhr intensive.
var run = function(){ if(offline.state=="up") offline.check(); }; setinterval(run, 20000); // check after 20sec offline.on('down',function(){ /**code handle network down*/ });
as pointed @adnan umer need set offline.options = { interceptrequests: true, requests: true }
intercept failed requests , try again once connection back. 1 caveat here requests resent offline out of scope of angular, accordingly, get
requests result nothing. fine, user initiates requests.
offline.options = { checks: {xhr: {url: "put_url_to_check_status_of_your_server"}} }
by default, offlinejs checks favicon.ico
.
Comments
Post a Comment