The GET parameters are the URL query string parameters. The Params addon
creates JSON using the URL query string parameters. The method getFromUrl
allows you to specify a GET parameter or get all of the GET parameters. You
can also use the alias url to get URL query string parameters.

For example, for the URL http://www.yahoo.com?foo=1&bar=2, the Params
addon would create the following object:

The POST parameters come from the HTTP POST request body and often consist of
form data. As with query string parameters, the Params addon has the method
getFromBody that allows you to specify a single parameter or get all of
the POST body parameters.

The Params addon also has the method getFromMerged that lets you get one or
all of the GET, POST, and routing parameters. Because all of the parameters are
merged into one collection, one parameter might be overridden by another with
the same key. You can also use the alias merged to
get one or all of the GET, POST, and routing parameters.

Thus, the parameter types are given the following priority:

routing parameters

GET parameters

POST parameters

For example, if each parameter type has a foo key, the foo routing
parameter will override both the GET and POST foo parameters.

We have looked at the methods of the Params addon for getting query string
parameter, query string parameters, and HTTP body data. For simplicity,
the Params addon also provides the aliases below
for the methods that we have covered thus far.

The method cookie.get(name) is used to get the cookie value associated
with name. In the example controller below, the cookie value
for 'user' is obtained and then used to pass user information to the
template.

After a mojit gets data, it may need to share that data with binders,
templates, or other mojits. Mojito provides the Data addon that allows your
mojit controllers and binders to share and access data. Data can also be
refreshed in templates using the Data addon and Handlebars expressions.

Data is passed from the server to the client and vice versa using a remote
procedural call (RPC) through a tunnel. During this transmission, the state of
the data is preserved.

When content is sent to the client as part of the page, the data and templates
are rendered on the server and sent to the client through the tunnel. After the
initial rendering, each time the mojit instance invokes an action that triggers
an RPC through the tunnel, data is serialized and sent to the server, where the
instance is recreated with the data. If the action changes the data model, the
new data is then sent back through the tunnel to the
client to update the data model.

The data sharing model used in Mojito is extremely flexible, allowing you to share data
in your application in many ways. We’ll look at how data is shared from the perspective
of controllers and binders, which have access to the Data addon.

When using the Data addon to share data, you should be aware of a couple of potential
issues. When multiple RPCs are made (e.g., multiple clicks on the same button), there is no
guarantee of the order of execution of the requests, which means you might get stale
data. Also, if your data model contains a lot of information, the payload of the RPC
will negatively affect performance and security as data is transmitted back and forth
between the client and server.

The Data addon is available through the ActionContext and mojitProxy objects
from the controller and binders respectively. Because the addon has two different
functions, allowing data to be shared from the server to the client and allowing one
mojit to share data with other mojits, the addon has the two objects data and
pageData.

As you saw in the Data Addon Objects table, the scope of the data object is
limited to a mojit. In other words, the controller can use the data object to share
data with its binder or templates, but not with other mojits. When you set data
with ac.data.set(key,value), the data is merged with the data passed to ac.done
(this is a shallow merge). The data is also serialized and rehydrated
on the client when the page is rendered in the browser.

From the controller, you use ac.data.set to set or expose data that the binder
can access with the mojitProxy object. The mojitProxy accesses the
set data with the data object as well with mojitProxy.data.get. Templates can
have Handlebars expressions to inject the set data into a page.

The following example shows you how you would set data and then access it from the binder
or the template.

The example below shows how a mojit controller can share stock price information
with its binder code and templates. This example shows how to access the shared data
with both Handlebars expressions and using mojitProxy.pageData in the binder.
In reality, you would only need to use one of these methods, with the former (Handlebars
expressions) being the preferred way.

YUI.add('stockquotes-binder-index',function(Y,NAME){Y.namespace('mojito.binders')[NAME]={init:function(mojitProxy){this.mojitProxy=mojitProxy;},bind:function(node){// From the mojitProxy, you use the data object to get the// value for stock_quotes that was set in the controller.varme=this,stock_quotes=this.mojitProxy.data.get('stock_quotes');this.node=node;varlist="<ul>";for(varsinstock_list){list+="<li>"+s+": $"+stock_list[s]+"</li>";}list+="</ul>";node.one('#stocks p').setHTML(list);}};},'0.0.1',{requires:['event-mouseenter','mojito-client']});

<divid="{{mojit_view_id}}"><h2>{{title}}</h2><ul><!-- The Handlebars block helper can iterate through the data made available through ac.data.set in the controller. -->
{{#each stock_quotes}}
<li>{{.}}</li>
{{/each}}
</ul><!-- Binder attaches the stock prices to the div container --><divid="stocks"><p></p></div></div>

Page data simply means data that is scoped to the mojits within a page.
The Data addon provides the pageData object, based on the
YUI Model API, which has a set method
for setting or exposing data that other mojits on the page can access through the
get method.

The pageData object is unique to each request, but is the one store for all mojits
of the request, allowing it to share data between mojits in a page. Binders
can access page data with mojitProxy.pageData.get(name). Templates can use
Handlebars to access page data as well, so the page data set with ac.pageData.set('name','foo')
from one mojit can be added the the template of another mojit with {{name}}.

The page object contains all of the page data set by controllers. Templates
can use {{page}} to access all of the available page data. The page
object is built on the server and then sent to the client, so the page data can
be shared and also re-hydrate the data on the page.

The pageData object serves as a mechanism for all mojits to access data in the
page object from the client or server. Both ac.pageData and mojitProxy.pageData
provide access to the same page model.

In the controller, when data is passed to ac.done or set through the data
or pageData namespace, Mojito wraps that data in the object this.page.
As a result of this wrapper, you can in fact also access data passed to ac.done or
set with the Data addon through this.page. For example, the data passed to the template with either
ac.done({stock_list:["YHOO","GOOG","CSCO"]}) or
ac.pagedata.set('stock_list',["YHOO","GOOG","CSCO"]) can be accessed in the template
with {{stock_list}} or {{this.page.stock_list}}.

In this example, we’re expanding on the idea of sharing stock price information.
The StockQuotes mojit shares the stock price quotes with other mojits
with the pageData.

As with previous example, we show how to access and attach shared data to the page with
the binder and the template, but in your applications, you would normally only
use one method, and the template approach is preferred.

YUI.add('stockquotes-binder-index',function(Y,NAME){Y.namespace('mojito.binders')[NAME]={init:function(mojitProxy){this.mojitProxy=mojitProxy;},bind:function(node){varticker=null;// From the mojitProxy, you use the data object to get the// value for stock_quotes that was set in the controller.this.mojitProxy.pageData.on('change',function(e){varticker=e.changed;}this.node=node;varlist="<ul>";for(varsinstock_list){list+="<li>"+s+": $"+stock_list[s]+"</li>";}list+="</ul>";node.one('#stocks p').setHTML(list);}};},'0.0.1',{requires:['event-mouseenter','mojito-client']});

In this binder, we are using an event handler to listen for updates to data. To listen
to changes to any data set, you can use mojitProxy.data.on('change',doSomething).
This example listens for changes to ticker_list.

<divid="{{mojit_view_id}}"><!-- Here we iterate through the data made available through ac.pageData.set in the controller of another mojit. Note: `stock_quotes` is wrapped in `this.page`, Thus you could use `{{#each this.page.stock_quotes}}`, too. -->
{{#each stock_quotes}}
<ahref="http://finance.yahoo.com/q?s={{.}}">{{.}}</a> |&nbsp;
{{/each}}
</div>