In Part 1, we got our basic application ready with the following setup:
Let us start with updating the model followed by the controller and binder in that order.
Below is the code for the function:
There are two ways to cook-up the controller:
And here's how our view looks like:
To glue it all, we need the binder. The binder, upon launch, will request to get all the items and update the UI. We create two functions in the binder:
Launch the application. It should, still, show same UI as earlier. There are no TODO items after all.
Monitor the console (Firebug or Chrome console) to ensure that there are no errors. Firebug console should show you log similar to as shown next:
In the next post, we implement the add behaviour to our application.
- Application and Routing Configuration
- TodoMVC defined assets
- Initial UI - HTML markup and CSS stylesheet
- Action – List all TODOs
- Action – Add a TODO
- Action – Edit a TODO
- Action – Mark one or all complete
- Action – Delete a TODO
Step 1: Action - List all TODOs
The first thing that we build up is retrieving all TODO items and showing them up. If there are no items in the list, then, as per the specs, the#main element must be hidden.Let us start with updating the model followed by the controller and binder in that order.
Updating the model
We add a functiongetAll.
Though, in our case, the storage is simple and synchronous, I would
rather prefer to write the method in the manner preferred in
asynchronous systems. The method requires no inputs to process. As such,
our function takes exactly one parameter - a callback function to be
invoked to return the result - failure or success.Below is the code for the function:
[code lang="js" highlight="14,15-22" title="todo.common.js"]
YUI.add('TodoMojitModelTodo', function(Y, NAME) {
var storage = new Y.CacheOffline();
Y.namespace('mojito.models').Todo = {
init: function(config) {
this.config = config;
var initData = storage.retrieve('todo');
if(!initData) {
storage.add('todo', []);
}
},
getAll: function(callback) {
if(storage) {
var obj = storage.retrieve('todo'),
rv = obj.response;
callback(null, rv);
} else {
callback('Storage not initialized');
}
}
}
}, "0.0.1", { requires: [ 'json', 'cache-offline' ] });
[/code]
There are two ways to cook-up the controller:
- One, create an action, say,
operate, that is implemented using command pattern. - Two, create multiple actions, one for each operation.
getAll in the controller as well. Additionally, we create another view - items.hb.html - that will be used to[code lang="js" title="controller.common.js" highlight="13-22,23"]
YUI.add('TodoMojit', function(Y, NAME) {
"use strict";
Y.namespace('mojito.controllers')[NAME] = {
index: function(ac) {
ac.assets.addBlob('<meta charset="utf-8">\n<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">', 'top');
ac.assets.addBlob('<link href="/static/' + ac.type + '/assets/base.css" rel="stylesheet" type="text/css" />', 'top');
ac.assets.addBlob('<!--[if IE]>\n<script src="/static/' + ac.type + '/assets/ie.js"></script>\n<![endif]-->', 'top');
ac.done({});
},
getAll: function(ac) {
ac.models.get('Todo').getAll(function(err, items) {
if(err) {
ac.error(err);
} else {
ac.done({
"items": items.reverse()
}, 'items');
}
});
}
}
}, '0.0.1', {requires: ['mojito', 'TodoMojitModelTodo',
'mojito-models-addon', 'json',
'mojito-assets-addon', 'mojito-params-addon']});
[/code]
And here's how our view looks like:
[code lang="html" title="views/items.hb.html"]
{{#items}}
<li id="{{id}}" class="{{#completed}}completed{{/completed}}">
<div class="view">
<input class="toggle" type="checkbox"{{#completed}} checked="checked"{{/completed}} />
<label>{{{title}}}</label>
<button class="destroy"></button>
</div>
<input class="edit" type="text" value="{{{title}}}" />
</li>
{{/items}}
[/code]
To glue it all, we need the binder. The binder, upon launch, will request to get all the items and update the UI. We create two functions in the binder:
resync: This function will fetch the markup for all the items and trigger UI refresh._resyncUI: This function will actually refresh the UI.
[code lang="js" title="binders/index.js" highlight="19,22-33,35-50" firstline="10"]
bind: function(node) {
var self = this;
this.node = node;
this.inputNode = node.one('#new-todo');
this.listNode = node.one('#todo-list');
this.toggleAll = node.one('#toggle-all');
this.main = node.one('#main');
self.resync();
},
resync: function() {
var self = this;
self.mp.invoke('getAll', { 'params': { }}, function(err, response) {
if(!err) {
self.listNode.set('innerHTML', '');
if(response) {
self.listNode.append(response);
}
self._resyncUI();
}
});
},
_resyncUI: function() {
var n = this.node,
toggles = n.all('.toggle'),
size = toggles.size(),
allSel = true,
i;
for(i = 0; i < size; i++) {
if(!toggles.item(i).get('checked')) {
allSel = false; break;
}
}
this.toggleAll.set('checked', size > 0 ? allSel : false);
this.main[size > 0 ? 'show': 'hide']();
}
};
}, '0.0.1', {requires: ['mojito-client', 'node', 'json', 'escape']});
[/code]
Launch the application. It should, still, show same UI as earlier. There are no TODO items after all.
Monitor the console (Firebug or Chrome console) to ensure that there are no errors. Firebug console should show you log similar to as shown next:
In the next post, we implement the add behaviour to our application.




