Created
July 24, 2012 17:09
-
-
Save gosukiwi/3171243 to your computer and use it in GitHub Desktop.
This file contains 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
(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