-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
juraj
committed
Jun 9, 2013
1 parent
cc89c48
commit b5e0381
Showing
7 changed files
with
972 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
angular.module('angular-json-rpc', []); | ||
// | ||
// json rpc for angular js. JSON-RPC-2.0 compatible. | ||
// spec - http://www.jsonrpc.org/specification | ||
// | ||
var jsonRpc = angular.module('angular-json-rpc', []).factory("jsonRpc", ['$http', function($http) { | ||
this.version = "2.0"; | ||
|
||
this.url = null; | ||
|
||
// | ||
// setup rpc | ||
// | ||
this.setup = function(params){ | ||
// check params | ||
check_params(params); | ||
this.url = params.url; | ||
return this; | ||
} | ||
|
||
|
||
success = function(data, status, headers, config){ | ||
|
||
} | ||
|
||
error = function(data, status, headers, config){ | ||
|
||
} | ||
|
||
// | ||
// json-rpc request | ||
// | ||
this.request = function(method, options, onSuccess, onFail){ | ||
if(options === undefined) | ||
options = { id: 1 }; | ||
|
||
if (options.id === undefined) | ||
options.id = 1; | ||
|
||
// make request | ||
var bodyRequest = JSON.stringify({"jsonrpc": this.version, "method": method, "params": options.params, "id" : options.id}); | ||
var headers = {'Content-Type': 'application/json'}; | ||
$http({'url': this.url, 'method': 'POST', 'data' : bodyRequest, 'headers': headers}) | ||
.success(function (data, status, headers, config){ | ||
if (data.error) { | ||
if (onFail) onFail(data.error); | ||
} else { | ||
if (onSuccess) onSuccess(data.result); | ||
} | ||
return data; | ||
}).error(function (data, status, headers, config) { | ||
if (onFail) onFail(data); | ||
return data; | ||
}); | ||
// return | ||
return true; | ||
} | ||
|
||
// | ||
// Check params | ||
// | ||
check_params = function(params){ | ||
if (params == undefined){ | ||
throw("Wrong params"); | ||
} | ||
|
||
if (typeof(params.url) !== 'string' || params.url == '') | ||
throw("Wrong url parameter"); | ||
} | ||
|
||
return this; | ||
|
||
}]); |
Oops, something went wrong.