The Object Structure

The generated JavaScript objects have 3 properties and 1 method.  The properties include:

  • "uri" – a string representing the path to the action method with no parameters
  • "query" – an array containing string values for each of the action method parameter names
  • "method" – either "GET" or "POST"

 

Building URLs

The generated JavaScript objects have a number of features to help make AJAX easier, readable, and more reliable.  Let's start with the most simple method of building URLs.


No Parameters

There are two ways you can create a URL without parameters.  If no parameters are necessary, the uri property is an easy way to get a URL.  You can also use the build() method with no parameters to find the same URL.

var url = app.endpoints.home.index.uri;
// or
var url = app.endpoints.home.index.build();

 

The resulting value is "/home/index".

 

A Singe Parameter

When your action method has a single parameter, you can simply pass the parameter to the object's build() method.  Assume there is a query parameter named "key."

var url = app.endpoints.home.index.build(9);

The resulting value is "/home/index?key=9".



Multiple Parameters

You'll need to use an array when passing multiple parameters.  By convention, the order of the parameters in your array must match the order of the parameter names in the object's "query" array.  Assume there are 3 query parameters named "id," "percent," and "message".

 

var url = app.endpoints.home.index.build([1, 99, "test"]);

The resulting value is "/home/index?id=1&percent=99&message=test".



Custom Parameter Names

The generated objects are not required to use the associated query parameters.  There may be situations where, perhaps because of custom routing, you want to pass in different parameter names to the same action method.

You can pass an object literal to the build() method, and each of the key/value pairs in the object literal will be converted into query string keys and values.  Even if the "index" endpoint has specific parameters, the object literal will override them and use the supplied values.

var url = app.endpoints.home.index.build({paramX: 15, paramY: 88);

The resulting value is "/home/index?paramX=15&paramY=88".

 

Accessing Endpoint Properties

Aside from the build() method, each object has 3 properties that can be used for deterministic or convenience purposes.


method

The "method" property can be used to determine what type of AJAX request to make.

var url = app.endpoints.home.index.build();

if(app.endpoints.home.index.method == "GET"){
    // jQuery get
    $.get(url);
}
else{
    // jQuery post
    $.post(url)
}

 

query

The query property contains an array of action method parameter names

var someObject = {};
for(var param in app.endpoints.home.index.query){
    someObject[param] = 123;
}


uri

the uri contains the base URL to an action method

var indexUrl = app.endpoints.home.index.uri;
console.log(indexUrl);

Last edited Apr 22, 2011 at 8:07 PM by tylerbrinks, version 2

Comments

No comments yet.