Thursday, November 26, 2015

Backbone.Collection

collections are ordered sets of models, where you can get and set models in the collection, listen for events when any element in the collection changes, and fetching for model’s data from the server

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>To-do App in Backbone.js</title>
 
  <!-- ========= -->
  <!--    CSS    -->
  <!-- ========= -->
  <style type="text/css">
    /* Hides bullet points from todo list */
    #todoapp ul {
      list-style-type: none;
    }
  </style> 
</head>
<body>
  <!-- ========= -->
  <!-- Your HTML -->
  <!-- ========= -->

    <section id="todoapp">
    <header id="header">
      <h1>Todos</h1>
      <input id="new-todo" placeholder="">
    </header>
    <section id="main">
      <ul id="todo-list"></ul>
    </section>
  </section>
 

 

  <!-- ========= -->
  <!-- Libraries -->
  <!-- ========= -->
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js" type="text/javascript"></script>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.0/backbone.localStorage-min.js" type="text/javascript"></script>

  <!-- =============== -->
  <!-- Javascript code -->
  <!-- =============== -->
  <script type="text/javascript">
    'use strict';

    var app = {}; // create namespace for our app
   
    //--------------
    // Models
    //--------------
    app.Todo = Backbone.Model.extend({
      defaults: {
        title: '',
        completed: false
      }
    });

    app.TodoList = Backbone.Collection.extend({
        model: app.Todo,
        localStorage: new Store("backbone-todo")
      });

      // instance of the Collection
      app.todoList = new app.TodoList();

   
  
    //--------------
    // Initializers
    //--------------  

    app.appView = new app.AppView();

  </script>
 
</body>
</html>

No comments:

Post a Comment