Created
August 20, 2013 13:13
-
-
Save manast/6281219 to your computer and use it in GitHub Desktop.
app.js for todomvc in gnd.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['gnd', 'models/todo', 'models/todoList', 'ctrls/todoListCtrl'], | |
function(Gnd, Todo, TodoList, TodoListCtrl){ | |
'use strict'; | |
// | |
// Create Local and Remote storages | |
// | |
var localStorage = new Gnd.Storage.Local(); | |
// | |
// Configure the synchronization queue. | |
// | |
Gnd.use.storageQueue(localStorage);//, remoteStorage); | |
var app = new Gnd.Base(); | |
// | |
// Lets get all the todo lists available (should be max one) | |
// We wait until we get the whole collection since we need to check its count. | |
// | |
TodoList.find().then(function(todoLists){ | |
if(todoLists.count == 0){ | |
// | |
// we have no todo lists yet, so lets create the one (the only one). | |
// | |
todoLists.add(new TodoList()); | |
} | |
// | |
// Pick the first element, which will be our todo list | |
// | |
var todoList = todoLists.first(); | |
todoList.keepSynced(); // autosync(enable = true) | |
function setFilter(all, active, completed) { | |
app.set('filterAll', all); | |
app.set('filterActive', active); | |
app.set('filterCompleted', completed); | |
} | |
setFilter(true, false, false); | |
// | |
// Bind the App model (only used for keeping the filter links updated) | |
// | |
var filtersViewModel = new Gnd.ViewModel(Gnd.$('#filters')[0], {app: app}); | |
// | |
// Get the todos collection | |
// | |
var todos = todoList.get('todos'); | |
var todoListCtrl = new TodoListCtrl(todos); | |
// | |
// Listen to available routes. Only used for selecting filters | |
// | |
Gnd.router.listen(function(req) { | |
req.get(function() { | |
if (req.isLast()) { | |
// default when no specifying any filter explicitly | |
todoListCtrl.showAll(); | |
setFilter(true, false, false); | |
} | |
req.get('active', '', function() { | |
todoListCtrl.showActive(); | |
setFilter(false, true, false); | |
}); | |
req.get('completed', '', function() { | |
todoListCtrl.showCompleted(); | |
setFilter(false, false, true); | |
}); | |
}); | |
}); | |
}); | |
}); // define |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment