-
-
Save b2l/1555f4d01f8246130e90 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
/** | |
* * Todos.js | |
* * contains a collection wrapper for the Todo model | |
* */ | |
// --- Data store | |
var _todos = []; | |
// --- Todo model | |
function Todo(name, done) { // function constructor, will be called by new Todo(xx, yy); | |
this.name = name; | |
this.done = done || false; | |
} | |
Todo.prototype.destroy = function() { | |
var index = _todos.indexOf(this); | |
_todos.splice(index, 1); | |
}; | |
Todo.prototype.markAsDone = function() { | |
this.done = true; | |
}; | |
Todo.prototype.markAsNotDone = function() { | |
this.done = false; | |
}; | |
// --- Todos collection | |
var Todos = { | |
getAll: function() { | |
return _todos; | |
}, | |
createTodo: function(name) { | |
var todo = new Todo(name); | |
_todos.push(todo); | |
return todo; | |
} | |
}; | |
// --- Expose Todos. | |
// If in node.js (or require.js), ```require('Todos.js')``` will return the Todos collection. | |
// If not, then Todos will be available as window.Todos | |
if (module && module.exports) { | |
module.exports = Todos; | |
} else { | |
window.Todos = Todos | |
} | |
/* | |
A better implementation will use the module pattern. | |
So that only what we put in the export (or window) is reachable | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment