The $.Controller
plugin helps to create an organized, memory leak-free JavaScript code.
A great example of how to use $.Controller
is the Todos
controller from Chapter 1, Getting Started with JavaScriptMVC, is as follows:
$.Controller('Todos', { // init method is called when new instance is created 'init': function (element, options) { this.element.html('todos.ejs', Todo.findAll()); }, // add event listener to strong element on click 'li strong click': function (el, e) { // trigger custom event el.trigger('selected', el.closest('li').model()); // log current model to the console console.log('li strong click', el.closest('.todo').model()); }, // add event listener to em element on click 'li .destroy click': function (el, e) { // call destroy on the model to prevent memoryleaking el.closest('.todo').model().destroy(); }, // add event listener to Todo model on destroyed '{Todo} destroyed': function (Todo, e, destroyedTodo) { // remove element from the DOM tree destroyedTodo.elements(this.element).remove(); console.log('destroyed: ', destroyedTodo); } });
18.226.98.208