javascript - Angularjs: Singleton service using $http -


Is it possible to create a service using $ HTTP, which is the method, URL, success and failure callback Take the form of the parameter when called from the controller.

I want to get the following type of functionality using angular

  var ajax = {URL: "webservice url", login: wrong, imported id: "", token: '', userdetails: new Backbone.collection.extend ({}), Server call: function (method, data, success callback, failurecol) {var that = this; //console.log(method); //console.log(successCallBack); That.showLoading (); $ .jax ({url: that.URL + method, method: 'post', data: data, // contentType: "application / json; charset = utf-8", success: function (data) {that.hideLoading ( ); If (that.checkForError (data)) {successCallBack (data);}}, failed: function (data) {that.hideLoading (); failurecol (data);}}); }   

I am using the folder structure inside the app and inside services that I have the code

  defined (['./ module' ], 'Service', 'service.factory' ('user_resources', ['$ resource', '$ location', function ($ resource, $ location) {return $ resource ("", {}} , {'GetAll': {Method: "GET", url: 'JSON / myList.JSON', isArray: true}})}}}))}};   

and I have the code in the controller

  define (['./module'], function (controller) {'strict use'; Controller Controller ('myListCtrl', ['Phone', 'Phone1', 'LoginForm', '$ scope', '$ http', 'user_resources', 'credit card', ceremony (phone, phone1, loginform, $ scope, $ Http, user_resources, credit card) {console.log (user_resources.getAll ()}}}})}}   

that gives [$ promise: object, $ solution: false] How to get data from that?

The factory ('User_Resource', ["$ resource", "$ location", function ($ resource, $ Location) {var baseUrl = $ location.protocol () + ": //" + $ location.host () + ($ Location.port () & amp; amp;:: + + location .port ()) + "/"; Return $ resource (Base URL + 'left / users / formerly /: id', {}, {query}: {method: 'method', 'iarra': true}, get: {Method: 'GET'}, login: {method: 'POST', url: baseUrl + 'rest / users / login'}, loginname: {method: 'POST' Url: baseUrl + 'rest / users / loginAnonymous'} , Logout: {method: 'POST', url: baseUrl + 'rest / users / Logout /: id '}, register: {method:' POST ', url: baseUrl +' rest / user / register '}});}];);

Usage example:

  userSrv.logout = function (user, success function, error function) {var userSrv = new User_Resource (); UserSrv. $ Logout ({user.id}, // Parameters function (data) {// Success Console Logs ("User Logout - Received"); console.log (data); if (success function! = Undefined) success Function (data);}, function (data) {// failure / error handling brings console here. Log ("user.logout - error received"); console.log (data); var errorMessage = "Connexion error" ; If (errorFunction! = Undefined) errorfunction (error message);}); }    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -