Skip to content

Instantly share code, notes, and snippets.

@b2l

b2l/Todos.js Secret

Last active August 29, 2015 14:05
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 b2l/1555f4d01f8246130e90 to your computer and use it in GitHub Desktop.
Save b2l/1555f4d01f8246130e90 to your computer and use it in GitHub Desktop.
/**
* * 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