I am in the process of creating a resource to send data to my controller for an existing API that I need to connect with. Unfortunately, I do not have the ability to make any changes on the backend.
Current state of my Resource factory:
'use strict';
angular.module('XXX')
.factory('elements', function (
$resource
) {
return $resource('http://XXX/api/v1/elements/:id',
{
callback: 'JSON_CALLBACK',
id: '@id'
},
{
query: {
method: 'JSONP',
params: {
id: '@id'
}
},
all: {
method: 'JSONP',
params: {}
}
}
);
});
The elements.query() functions as expected, but unfortunately the elements.all() is not working. Upon inspecting the returned content in the network tab, I noticed that it starts with angular.callbacks._2([{... DATA...}]), which does not seem correct to me.
UPDATE.....
After some modifications, I managed to get it to work with this:
angular.module('XXX')
.factory('element', function (
$resource
) {
return $resource('http://XXX/api/v1/elements/:id',
{
id: '@id',
callback : 'JSON_CALLBACK',
},
{
query: {
method: 'JSONP',
params: {
id: '@id'
}
},
all: {
method: 'JSONP',
isArray: true,
params: {
callback : 'JSON_CALLBACK',
}
}
}
);
});
However, the JSON it returns comes in as an array. While I can parse it for usage, I am now questioning if this is the best practice?