This is Part 2 of the "Hello World" article. In this followup article, we enhance our code to:
Let's modify the file to reflect a form:
With this content, if you restart the server, you should see a form as below:
Great. Just type in your name and hit the Submit button... what do you see?
Well, I think this is pretty much expected and I also think that you know what to fix. Open the routes.json and update the "verbs" to include "POST" as well. The final contents of routes.json should be as below:
Now, if you restart the server and click on the Submit button, it should not give an error but should show the form again.
It's now time to bring the form in action...

Mojito provides a whole bunch of "addons" (a.k.a. plugins) that can be optionally used in any mojit. We will use the "params" addon that allows me to parse incoming data (parameters).
The business logic - including parsing incoming data, error handling etc - has to be in the controller. Because we are submitting the form to "/" which is mapped to "hello_mojit.index" in routes.json, the action / function that needs update is the controller-index function. Update the controller to reflect the following code:
And subsequently, we need to update the view as well... so that message is displayed:
With all these changes in place, restart the server, browse to the page, type in your name and click submit to see the results:
To summarize, in this article, we looked at:
- Render a form on the landing page which asks user to enter his/her name
- Process the data on the submitted form
- Say "Hello {user}" rather than "Hello World"
- One of the various possible options to accept data to the server
- Process incoming data
- Apply business logic and create the data to be rendered
1. Customizing View to create a Form
If you recollect from the earlier article, the index-view resides in {mojit-dir}/views/index.mu.html.Let's modify the file to reflect a form:
[code lang="html"]<div id="{{mojit_view_id}}">
<form method='post' action='/'>
<div>Enter Name: <input type='text' name='name' /></div>
<div><input type='submit' value='Submit' /></div>
</form>
</div>
[/code]
With this content, if you restart the server, you should see a form as below:
Great. Just type in your name and hit the Submit button... what do you see?
Well, I think this is pretty much expected and I also think that you know what to fix. Open the routes.json and update the "verbs" to include "POST" as well. The final contents of routes.json should be as below:
[code lang="js" highlight="5"][{
"settings": [ "master" ],
"root": {
"path": "/",
"verbs": [ "GET", "POST" ],
"call": "hello_mojit.index"
}
}][/code]
Now, if you restart the server and click on the Submit button, it should not give an error but should show the form again.
It's now time to bring the form in action...
2. Process the incoming data
Note that the technique that we are using in our case to accept incoming data is the traditional form submit. All we need to have is access to the value for the key named "name" (see form earlier).Mojito provides a whole bunch of "addons" (a.k.a. plugins) that can be optionally used in any mojit. We will use the "params" addon that allows me to parse incoming data (parameters).
The business logic - including parsing incoming data, error handling etc - has to be in the controller. Because we are submitting the form to "/" which is mapped to "hello_mojit.index" in routes.json, the action / function that needs update is the controller-index function. Update the controller to reflect the following code:
[code lang="js" highlight="9-12,18"]YUI.add('HelloMojit', function(Y, NAME) {
Y.mojito.controllers[NAME] = {
init: function(config) {
this.config = config;
},
index: function(ac) {
// You can use ac.params.url('key') for HTTP-GET parameters
var name = ac.params.body('name');
var msg = name ? "Hello " + name : '';
ac.done({ "message": msg });
}
};
}, '0.0.1', {requires: ['mojito', 'HelloMojitModelFoo',
'mojito-params-addon']});
[/code]
And subsequently, we need to update the view as well... so that message is displayed:
[code lang="html" highlight="7,8,9"]
<div id="{{mojit_view_id}}">
<form method='post' action='/'>
<div>Enter Name: <input type='text' name='name' /></div>
<div><input type='submit' value='Submit' /></div>
</form>
<div>
{{message}}
</div>
</div>[/code]
With all these changes in place, restart the server, browse to the page, type in your name and click submit to see the results:
To summarize, in this article, we looked at:
- Accepting data through form post (configuring routes.json)
- Processing incoming data in controller




No comments:
Post a Comment