Service

Class representation of a web service call.

Summary
Class representation of a web service call.
Creates an instance of the mojo.service.Service class.
Retreives the name of the web service.
Sets the name for the web service.
Retrieves the Uri for the web service.
Sets the Uri for the web service.
Retrieves the options for the web service call.
Sets the options for the web service call.
Fires the web service request, and triggers onResponse or onError callback methods on the caller depending on success or failure respectively.

Constants

mojo. service. Service constants

VALID_METHODSA list of HTTP methods acceptable as configuration when instantiating a new mojo.service.Service object.
DEFAULT_PARAMSThe default configuration for a mojo.service.Service object.

Functions

constructor

constructor: function(name,
uri,
paramsObj)

Creates an instance of the mojo.service.Service class.

Parameters

name{string}
uri{string}
paramsObj{object}

Example

// instantiate a new JSON service that caches for 60 seconds
var jsonService = new mojo.service.Service("getRSS", "/json/rssFeed", {format: 'json', cache: true, cacheExpiry: 60});

// instantiate a new text transport service that does not cache
var textService = new mojo.service.Service("getHTMlFragment", "/html/fragment.html", {format: 'text', cache: false});

// instantiate a new XML service that does not make inferences about what nodes should form part of an array of nodes
var xmlService = new mojo.service.Service("getAtom", "/xml/atomFeed", {format: 'xml', inferArrays: false});

getName

getName: function()

Retreives the name of the web service.

Returns

{string} Name of the service.

Example

var service = new mojo.service.Service("getRSS", "/json/rssFeed", {format: 'json', cache: true});
var name = service.getName(); // returns "getRSS"

setName

setName: function(name)

Sets the name for the web service.

Parameters

name{string} The name of the service.

Example

// instantiate a new service
var service = new mojo.service.Service("getRSS", "/json/rssFeed", {format: 'json', cache: true});
service.setName("getNews"); // changes name from "getRSS" to "getNews"

getUri

getUri: function()

Retrieves the Uri for the web service.  Uri returned will include TrimPath variable syntax if inserted.

Returns

{string} uri

Example

// instantiate a new service
var service = new mojo.service.Service("getRSS", "/json/rssFeed", {format: 'json', cache: true});
var uri = service.getUri(); // returns "/json/rssFeed"

setUri

setUri: function(uri)

Sets the Uri for the web service.  TrimPath variable syntax can be inserted to allow for dynamic Uri generation when invoking service call.

Parameters

uri{string}

Example

// instantiate a new service
var service = new mojo.service.Service("getRSS", "/json/rssFeed", {format: 'json', cache: true});
service.setUri("/json/rssFeed/${id}"); // add dynamic variable to Uri

getParams

getParams: function()

Retrieves the options for the web service call.  If options are not explicitly set, default option values will be returned.

Returns

{object} Parameter Object.

Example

// instantiate a new service
var service = new mojo.service.Service("getRSS", "/json/rssFeed", {format: 'json', cache: true});
var options = service.getParams(); // returns {json:true, cache:true, method:"GET", cacheExpiry:0, retry: 1}

setParams

setParams: function(paramsObj)

Sets the options for the web service call.

Parameters

paramsObj{object} Parameters to be sent to the service.

Example

// instantiate a new service
var service = new mojo.service.Service("getRSS", "/json/rssFeed", {format: 'json', cache: true});
service.setParams({cache: false}); // changes cache to false. All other defined options remain unchanged.

invoke

invoke: function(paramsObj,
callerObj)

Fires the web service request, and triggers onResponse or onError callback methods on the caller depending on success or failure respectively.  Optional service parameters are sent with the web service request, and are used to generate a dynamic Uri for the call when TrimPath syntax is used (if TrimPath.parseTemplate is available).

Parameters

paramsObj{object} Parameters to send to the service.
callerObj{object} Command object to pass to the service.

Example

// instantiate a new service
var service = new mojo.service.Service("getRSS", "/json/rssFeed/${id}", {format: 'json', cache: true});
var caller = {
onResponse: function(data) { alert("success!"); },
onError: function(error) { alert("error"); }
}
service.invoke({id: "cnn"}, caller); // generates Uri "/json/rssFeed/cnn" based on the service parameters passed in.
//Triggers callback methods onResponse or onError
constructor: function(name,
uri,
paramsObj)
Creates an instance of the mojo.service.Service class.
getName: function()
Retreives the name of the web service.
setName: function(name)
Sets the name for the web service.
getUri: function()
Retrieves the Uri for the web service.
setUri: function(uri)
Sets the Uri for the web service.
getParams: function()
Retrieves the options for the web service call.
setParams: function(paramsObj)
Sets the options for the web service call.
invoke: function(paramsObj,
callerObj)
Fires the web service request, and triggers onResponse or onError callback methods on the caller depending on success or failure respectively.