/framework/js/todos.js
https://github.com/dtuite/backbone-presentation-part-2 · JavaScript · 154 lines · 69 code · 37 blank · 48 comment · 1 complexity · 3be8f2c42ad6e320f0237149b703c54c MD5 · raw file
- // An example Backbone application contributed by
- // [Jérôme Gravel-Niquet](http://jgn.me/). This demo uses a simple
- // [LocalStorage adapter](backbone-localstorage.html)
- // to persist Backbone models within your browser.
- // Load the application once the DOM is ready, using `jQuery.ready`:
- $(function(){
- // Todo Model
- // ----------
- // Our basic **Todo** model has `text`, `order`, and `done` attributes.
- window.Todo = Backbone.Model.extend({
- // Default attributes for a todo item.
- defaults: function() {
- return {
- done: false,
- order: Todos.nextOrder()
- };
- },
-
- // Toggle the `done` state of this todo item.
- toggle: function() {
- this.save({done: !this.get("done")});
- }
- });
- // Todo Collection
- // ---------------
- // The collection of todos is backed by *localStorage* instead of a remote
- // server.
- window.TodoList = Backbone.Collection.extend({
- // Reference to this collection's model.
- model: Todo,
- // Save all of the todo items under the `"todos"` namespace.
- localStorage: new Store("todos"),
- // Filter down the list of all todo items that are finished.
- done: function() {
- // Looks through each value in the list, returning an array of all the values that pass a truth test
- return this.filter(function(todo){ return todo.get('done'); });
- },
- // Filter down the list to only todo items that are still not finished.
- remaining: function() {
- // Returns a copy of the array with all instances of the values removed
- return this.without.apply(this, this.done());
- },
- // We keep the Todos in sequential order, despite being saved by unordered
- // GUID in the database. This generates the next order number for new items.
- nextOrder: function() {
- if (!this.length) return 1;
- return this.last().get('order') + 1;
- },
- // Todos are sorted by their original insertion order.
- // comparators are used to maintain order in a collection
- // this comparator orders todo's based on the value of the 'order' attribute
- comparator: function(todo) {
- return todo.get('order');
- }
- });
- // Create our global collection of **Todos**.
- window.Todos = new TodoList;
- // Todo Item View
- // --------------
- // The DOM element for a todo item...
- window.TodoView = Backbone.View.extend({
- //... is a list tag.
- tagName: "li",
- // Cache the template function for a single item.
- template: _.template($('#item-template').html()),
- // The TodoView listens for changes to its model, re-rendering.
- initialize: function() {
- },
- // Re-render the contents of the todo item.
- render: function() {
- $(this.el).html(this.template(this.model.toJSON()));
- this.setText();
- return this;
- },
- // To avoid XSS (not that it would be harmful in this particular app),
- // we use `jQuery.text` to set the contents of the todo item.
- setText: function() {
- var text = this.model.get('text');
- this.$('.todo-text').text(text);
- this.input = this.$('.todo-input');
- },
- // Remove this view from the DOM.
- remove: function() {
- $(this.el).remove();
- },
- });
- // The Application
- // ---------------
- // Our overall **AppView** is the top-level piece of UI.
- window.AppView = Backbone.View.extend({
- // Instead of generating a new element, bind to the existing skeleton of
- // the App already present in the HTML.
- el: $("#todoapp"),
- // At initialization we bind to the relevant events on the `Todos`
- // collection, when items are added or changed. Kick things off by
- // loading any preexisting todos that might be saved in *localStorage*.
- initialize: function() {
- this.input = this.$("#new-todo");
- Todos.bind('add', this.addOne, this);
- Todos.bind('reset', this.addAll, this);
- Todos.bind('all', this.render, this);
- Todos.fetch();
- },
- render: function() {},
- // Add a single todo item to the list by creating a view for it, and
- // appending its element to the `<ul>`.
- addOne: function(todo) {
- var view = new TodoView({model: todo});
- this.$("#todo-list").append(view.render().el);
- },
- // Add all items in the **Todos** collection at once.
- addAll: function() {
- Todos.each(this.addOne);
- },
-
- // put the createOnEnter fn here
- });
- window.App = new AppView();
- });