Skip to content

Instantly share code, notes, and snippets.

@gosukiwi
Created July 24, 2012 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gosukiwi/3171243 to your computer and use it in GitHub Desktop.
Save gosukiwi/3171243 to your computer and use it in GitHub Desktop.
(function () {
"use strict";
var Todos = [];
// Model - Entities
var Todo = function (options) {
this.title = options.title;
this.isDone = options.isDone;
};
// View - Everything related with DOM
var TodoView = function () { }
TodoView.prototype.addEvent = function (selector, name, cb, context) {
$(selector).bind(name, { me: context }, cb);
}
TodoView.prototype.addItem = function (title) {
$('#todo-list').append('<li>' + title + '</li>');
}
TodoView.prototype.updateCount = function (count) {
$('#total-todos').html(count);
}
TodoView.prototype.clearTitle = function () {
$('#todo-title').val('').focus();
}
// Controller - All logic
var TodoController = function (view) {
this.view = view;
this.view.addEvent('#btn-add', 'click', this.addTodo, this);
};
TodoController.prototype.addTodo = function (e) {
var title = $('#todo-title');
var me = e.data.me;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "TodoManager.asmx/AddTodo",
dataType: "json",
data: '{ "title": "' + title.val() + '", "isDone": false }',
success: function (data) {
var todo = new Todo(data.d);
Todos.push(todo);
me.view.addItem(todo.title);
me.view.updateCount(Todos.length);
me.view.clearTitle();
}
});
}
$(document).ready(function () {
var todoView = new TodoView();
var todoController = new TodoController(todoView); // Pass the view to the controller
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment