javascript - 尝试重置集合时出现 Backbone 错误

标签 javascript backbone.js

我正在做一个非常简单的 Backbone 应用程序示例,但我不断收到 JS 错误消息。我基本上有两个文件:

  1. app.js

此文件创建一个 Backbone.js 应用程序,该应用程序使用 Collection View 中的数据将模板中的跨度附加到 html 文件上已创建的 div。

    /*Backbone.js Appointments App*/

    App = (function($){
      //Create Appointment Model
      var Appointment = Backbone.Model.extend({});

      //Create Appointments Collection
      var Appointments = Backbone.Collection.extend({
        model: Appointment
      });

      //Instantiate Appointments Collection
      var appointmentList = new Appointments();
      appointmentList.reset(
        [{startDate: '2013-01-11', title: 'First Appointment', description: 'None', id: 1},
         {startDate: '2013-02-21', title: 'Second Appointment', description: 'None', id: 2},
         {startDate: '2013-02-26', title: 'Third Appointment', description: 'None', id: 3}
        ]
      );

      //Create Appointment View
      var AppointmentView = Backbone.View.extend({
        template: _.template(
          '<span class="appointment" title="<%= description %>">' +
          ' <span class="title"><%= title %></span>' +
          ' <span class="delete">X</span>' +
          '</span>'
        ),

        initialize: function(options) {
          this.container = $('#container');
        },

        render: function() {
          $(this.el).html(this.template(this.model));
          this.container.append(this.el);
          return this;
        }
      });

      var self = {};
      self.start = function(){
        new AppointmentView({collection: appointmentList}).render();
      };
      return self;
    });

    $(function(){
      new App(jQuery).start();
    });
  1. index.html

此文件仅调用 jquery、backbone 和其他 js 库,并创建了 div 容器,前一个文件中的 Backbone.js 应用程序将在其中附加数据。

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>hello-backbonejs</title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
      <script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
      <script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js"></script>
      <script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>
      <script src="app.js" type="text/javascript"></script>
    </head>
    <body>
    <div id="container"></div>
    </body>
    </html>

我得到的错误如下:

Uncaught TypeError: Object [object Object] has no method 'reset' app.js:14
App app.js:14
(anonymous function) app.js:49
e.resolveWith jquery.min.js:16
e.extend.ready jquery.min.js:16
c.addEventListener.z jquery.min.js:16

最佳答案

您使用的是 Backbone 0.3.3,但 Collection.reset 是在 Backbone 0.5 中引入的。请参阅changelog了解更多信息。

要么升级 Backbone(目前为 0.9.9)(以及 Underscore 和 jQuery),要么使用 Collection#refresh 如果您绝对必须保留 Backbone 0.3.3(但是您可能会遇到其他错误)。

关于javascript - 尝试重置集合时出现 Backbone 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14277097/

相关文章:

javascript - 下划线用换行符替换新行字符

javascript - jquery 如何在单击按钮时停止 $.ajax

javascript - 在 Meteor 中执行大型操作

jquery - D3.js 仅在一行上绘制条形图矩形

javascript - 删除集合中的模型并触发删除事件-backbone.js

backbone.js - 如何从我的主干 View 中取消绑定(bind)所有 socket.io 事件?

php - 将 $_GET 变量分配给 JS 变量

javascript - 在 firestore 中查找具有 id 的特定字段

javascript - 程序卡在 clearInterval()

jquery - 如何在 Backbone.js 中加载具有外键关系的子模型?