angularjs - Allowing variable length arguments to Angular's $resource? -


here's issue, have multiple api calls make along lines of:

www.domain.com/foo/bar?token=12345

there may more subdirectories in-between, there may less.

i'm using $resource

agentapp.factory('apifactory', function($resource) {     return $resource('www.domain.com/v1/' + ':folder',      {         'query': {             method: 'get',             isarray: true     } }); 

which gets called follows:

apifactory.query({folder: 'foo', token: '12345'}, function() {...}) 

i make more extensible have need change isarray value false, , amount of subdirectories in url unknown on occasion, i'd prefer not use $resource's :token structure, , rather have 1 takes string.

is there service create allow me make call follows:

apiservice.query(urlstringandqueries, booleanforisarray).then(function(response) { ...do response }); 

so far i've attempted following, doesn't give me want, i'm not sure how $resource kick off api call, put down fundamental misunderstanding of how $resource works:

agentapp.factory('apiservice', ['$resource', '$q', function ($resource, $q) {     var factory = {         query: function (urlstringandqueries, isarray) {             return $q(                 function() {                     $resource('www.domain.com/v1/' + ':location', { location: urlstringandqueries }, {                         'query': {                             method: 'get',                             isarray: isarray                         }                     });                 }             )         },     return factory; }]); 

which attempt call follows:

apiservice.query('/foo/bar?token=12345', true)     .then(function(response) { ...do response }); 

any and/or advice appreciated. thanks!

edit: here's solution until more generic pattern comes along

i couldn't provide entity base url string externally, e.g. foo/bar?token=12345, due $response inherently encoding url, characters (/?=) converted encoded counterparts. hence separating of strings in pattern:

agentapp.factory('apiservice', ['$resource', function($resource) {     var factory = {         resourceobj: function(isarray, urlstringandqueries) {              /* urlstringandqueries variadic parameters,             deconstruct them use in url */              var location1, location2, location3, locations = [];              angular.foreach(arguments, function(path) {                 locations.push(path);             });              return $resource(vapiurl + vapiver + '/' + ':location1' + '/' + ':location2' + '/' + ':location3' + '/' + ':location4', {                 location1: locations[1],                 location2: locations[2],                 location3: locations[3],                 location4: locations[4],             }, {                 'query': {                     method: 'get',                     isarray: isarray                 },                 'save': {                     method: 'post',                     isarray: isarray                 }             })         }     };     return factory; }]); 

this solution still assumes i'll have finite amount of paths, isn't ideal @ least it's easy reuse.

still open more , better suggestions :)

i don't understand why returning promise object service method again, while $resource return promise itself. don't

code

agentapp.factory('apiservice', ['$resource', '$q', function($resource, $q) {     var factory = {         resourceobj: function(urlstringandqueries, isarray) {             return $resource('www.domain.com/v1/' + ':location', {                 location: urlstringandqueries             }, {                 'query': {                     method: 'get',                     isarray: isarray                 }             });         )     } }]); 

call factory method you'll access resource object call query of resource object method

controller

apiservice.resourceobj('/foo/bar?token=12345', true).query()     .then(function(response) { ...do response }); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -