JSON-RPC (v2.0) for AngularJS.
The jsonrpc
service includes some shorthand methods to quickly create simple
services. The created service methods accept data and optional $http.Config
and return $http.HttpPromise
.
module = angular.module('xyz', ['jsonrpc']);
module.service('locationService', function(jsonrpc) {
var service = jsonrpc.newService('locationsvc');
this.get = service.createMethod('Get');
this.save = service.createMethod('Save');
});
This way, the service can be injected into controllers, etc.
module.controller('Ctrl', function($scope, locationService) {
$scope.coords = [];
locationService.get({max: 10}).success(function(data) {
$scope.coords = data.coords;
});
locationService.save({lat: 22, long: 33}, {headers: {'X-ACL': 'x@y.z'}}).
success(function(data) {}).
error(function(error) {});
});
The methods used are:
jsonrpc.request('svc.Get', {x: "y"}, {}); // returns $http.HttpPromise
jsonrpc.request('/_goRPC_', 'svc.Get', {x: "y"}, {});
which internally call
jsonrpc({path: '/_goRPC_', method: 'svc.Get', data: {x: "y"}}, {});
(the last object in each of these calls is an optional $http.Config
object)
The base RPC path can be configured using the jsonrpcProvider
module.config(function(jsonrpcProvider) {
jsonrpcProvider.setBasePath('http://localhost:8000/rpc');
});
- Use $q and resolve the result to a value directly, like $resource.
- Make services more configurable through
jsonrpc.newService()
. - Better tests.
MIT open-source licence