骨干视图/模板混淆

时间:2013-09-16 03:54:43

标签: backbone.js

我正在将这个http://localtodos.com/ todo示例转换为通讯录,但结果却比我想象的更难。我将数据保存到localstorage但我似乎无法显示模板。实际上它似乎根本不想触及AddressBookView。当我在浏览器中点击索引并添加新联系人以保存它时,不会显示新的联系人或以前保存的联系人。代码如下。谢谢你的帮助。

$(function(){

  var AddressBook = Backbone.Model.extend({

    defaults: function() {
      return {
         name: "No contact name provided",
         email: "No email address provided"
      };
    },
  });

  // Todo Collection
  // ---------------

  var AddressBookList = Backbone.Collection.extend({

    model: AddressBook,

    // Save all of the todo items under the `"todos-backbone"` namespace.
    // localStorage: new Backbone.LocalStorage("todos-backbone"),
    localStorage: new Backbone.LocalStorage("address-book"),

    // 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.
    comparator: 'order'

  });

  // Create our global collection of **Todos**.
  var AddressBookEntries = new AddressBookList;

  // Todo Item View
  // --------------

  // The DOM element for a todo item...
  var AddressBookView = Backbone.View.extend({

    tagName:  "li",

    // Cache the template function for a single item.
    template: _.template($('#item-template').html()),

    // The DOM events specific to an item.
    events: {
      "click .toggle"   : "toggleDone",
      "dblclick .view"  : "edit",
      "click a.destroy" : "clear",
      "keypress .edit"  : "updateOnEnter",
      "blur .edit"      : "close"
    },

    // The TodoView listens for changes to its model, re-rendering. Since there's
    // a one-to-one correspondence between a **Todo** and a **TodoView** in this
    // app, we set a direct reference on the model for convenience.
    initialize: function() {
        alert("TESST");
      this.listenTo(this.model, 'change', this.render);
      this.listenTo(this.model, 'destroy', this.remove);
    },

    // Re-render the titles of the todo item.
    render: function() {
      console.log("TEST")

      this.$el.html(this.template(this.model.toJSON()));
      this.input = this.$('.edit');
      return this;
    },

    // Toggle the `"done"` state of the model.
    toggleDone: function() {
      this.model.toggle();
    },

    // Switch this view into `"editing"` mode, displaying the input field.
    edit: function() {
      this.$el.addClass("editing");
      this.input.focus();
    },

    // Close the `"editing"` mode, saving changes to the todo.
    close: function() {
      var value = this.input.val();
      if (!value) {
        this.clear();
      } else {
        this.model.save({email: value});
        this.model.save({name: value});
        this.$el.removeClass("editing");
      }
    },

    // If you hit `enter`, we're through editing the item.
    updateOnEnter: function(e) {
      if (e.keyCode == 13) this.close();
    },

    // Remove the item, destroy the model.
    clear: function() {
      this.model.destroy();
    }

  });

  // The Application
  // ---------------

  // Our overall **AppView** is the top-level piece of UI.
  var AppView = Backbone.View.extend({

    // Instead of generating a new element, bind to the existing skeleton of
    // the App already present in the HTML.
    el: $("#address-book"),

    // Our template for the line of statistics at the bottom of the app.
    //statsTemplate: _.template($('#stats-template').html()),

    // Delegated events for creating new items, and clearing completed ones.
    events: {
      "click #save-entry":  "createAddressBookEntry",
      "click #clear-completed": "clearCompleted",
      "click #toggle-all": "toggleAllComplete"
    },

    // 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.$("#save-entry");
      // this.allCheckbox = this.$("#toggle-all")[0];

      this.listenTo(AddressBookEntries, 'add', $("#name").addOne);
      this.listenTo(AddressBookEntries, 'reset', this.addAll);
      this.listenTo(AddressBookEntries, 'all', this.render);

      this.footer = this.$('footer');
      this.main = $('#main');

      AddressBookEntries.fetch();
    },

    // Re-rendering the App just means refreshing the statistics -- the rest
    // of the app doesn't change.
    render: function() {
      // var done = AddressBookEntries.done().length;
      // var remaining = AddressBookEntries.remaining().length;

      if (AddressBookEntries.length) {
        this.main.show();
        this.footer.show();
        // this.footer.html(this.statsTemplate({done: done, remaining: remaining}));
      } else {
        this.main.hide();
        this.footer.hide();
      }

      // this.allCheckbox.checked = !remaining;
    },

    // Add a single todo item to the list by creating a view for it, and
    // appending its element to the `<ul>`.
    addOne: function(entry) {
      console.log("Second This" + this);
      var view = new AddressBookView({model: entry});
      this.$("#contact-list").append(view.render().el);
    },

    // Add all items in the **Todos** collection at once.
    addAll: function() {
      AddressBookEntries.each(this.addOne, this);
    },

    // If you hit return in the main input field, create new **Todo** model,
    // persisting it to *localStorage*.
    createAddressBookEntry: function(e) {

      AddressBookEntries.create({name: $("#name").val()});
      AddressBookEntries.create({email: $("#email").val()});
      $("#email").val('');
      $("#name").val('');
    },
  });

  // Finally, we kick things off by creating the **App**.
  var App = new AppView;

});

的index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Backbone.js Todos</title>
  <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" media="screen" title="no title" charset="utf-8">
  <link rel="stylesheet" href="css/narrowJumbotron.css" type="text/css" media="screen" title="no title" charset="utf-8">
  <link rel="stylesheet" href="css/app.css" type="text/css" media="screen" title="no title" charset="utf-8">
</head>

<body>

    <div class="container">
          <div class="header">
            <ul class="nav nav-pills pull-right">
              <li class="active"><a href="#">Home</a></li>
              <li><a href="#">About</a></li>
              <li><a href="#">Contact</a></li>
            </ul>
            <h3 class="text-muted">Address Book</h3>
          </div>

          <div class="jumbotron">
            <h1>Jumbotron heading</h1>
            <p class="lead"></p>
            <p><a class="btn btn-lg btn-success" href="#">Sign up today</a></p>
          </div>

          <div class="row marketing">
            <div class="col-lg-6">
                <div id="address-book">

                  <header>
                  <input id="name" type="text" placeholder="Name">
                  <input id="email" type="text" placeholder="Email">
                  <a id="save-entry" class="btn btn-lg btn-success" href="#">Save</a>
                  </header>

                  <section id="main">
                    <ul id="contact-list"></ul>
                  </section>
                </div>

            </div>
        </div>
    </div>

  <script src="js/jquery-1.10.1.min.js" type="text/javascript" charset="utf-8"></script>
  <script src="js/underscore.js" type="text/javascript" charset="utf-8"></script>
  <script src="js/backbone.js" type="text/javascript" charset="utf-8"></script>
  <script src="js/backbone.localStorage-min.js" type="text/javascript" charset="utf-8"></script>
  <script src="js/app.js" type="text/javascript" charset="utf-8"></script>

  <!-- Templates -->

  <script type="text/template" id="item-template">
    <div class="view">
      // <input class="toggle" type="checkbox" <%= done ? 'checked="checked"' : '' %> />
      <label><%- name %></label>
      <a class="destroy"></a>
    </div>
    <input class="edit" type="text" value="<%- name %>" />
  </script>

  </body>
</html>

1 个答案:

答案 0 :(得分:2)

你说

this.listenTo(AddressBookEntries, 'add', $("#name").addOne);

我认为你的意思是

this.listenTo(AddressBookEntries, 'add', this.addOne);

您还希望保存此类型号

    this.model.save({email: value, name: value});

否则会添加不必要的渲染,并会两次触发change事件。