URI

In a typical, back-end MVC architecture (in PHP, for example) all requests to certain URIs (or URLs) are handled by a controller, and a view is returned (if the application isn't purely an API that returns raw data).


In Maverick, the URI can represent the state of the application but necessarily does not have to. You can listen to the 'uri.changed' event from the controllers and make sure the URI always represents the state of the application but you can completely ignore the URI and only work with controllers via your own custom events.


However, URI is good for a couple of things. The main benefit of updating the URI is that users can link to the specific state of the application by copy-pasting the current URI. Secondly, updating the URI enables Back-Forward navigation of the web browser which is something people are used with.


$uri.setBase()

Sets a base URI that represents the "index" location of an application. By default, this is set to document.location.href but you should always define it from your application if you're using URI based navigation.
Arguments: [string] baseUri
$uri.setBase('http://www.mywebapp.com');

Ideally you should call this method only once — in the start of your application.

$uri.setContainer()

Lets you change where the URI is contained (either 'history' or 'hash').
Arguments: [string] container
$uri.setContainer('history');


The 'history' is supported by newer browsers. The 'history' switches the application URI as if the user navigated to a new page but without a page refresh. The 'hash' simply updates the document location hash and grabs the current URI from the hash.

Ideally you should call this method only once — in the start of your application.


$uri.goTo()

Changes the current URI / navigates to a new URI (does not refresh the page) + triggers 'uri.changed' event
$uri.goTo('article', { key: 'value', key2: 'value2', key3: { subkey1: 'subvalue' } });

$uri.getSegment()

Returns the nth segment of the current URI as string
Arguments: [integer] n
For example, if the application URI is set to /articles/view/5124/republicans-voted-in-favor, then "articles" is the first, "view" is the second, "5124" is the third segment of the URI ("republicans-voted-in-favor" is the fourth).
$uri.getSegment(2);

$uri.getSegments()

Returns the nth segment of the current URI as string
Arguments: [integer] start, [integer] stop
For example, if you ask for segments 2 to 4, the function would return "view/5124/republicans-voted-in-favor"
$uri.getSegments(2,4);

$uri.asObj()

Returns the current URI as (key-value) object
$uri.asObj();

$uri.fromObj()

Constructs an URI string from (key-value) object and returns it
Arguments: [object] obj
$uri.asObj();


$uri.asString()

Returns the current URI as string
$uri.asString();


$uri.getTotalSegments()

Returns the number of current URI segments.
For example, if the application URI is set to /articles/view/5124/republicans-voted-in-favor, then the number of total URI segments is 4.
$uri.getTotalSegments();


$uri.asArray()

Returns the current URI segments as array
$uri.asArray();


Listening to URI changes

Whenever the URI changes, Maverick fires the 'uri.changed' event which you can listen to from view or controller scope:

this.listen('uri.changed', function() { alert('URI changed! o/'); });