0

我正在尝试将Django 和tastepie 与backbone.js 和mustache 一起使用。我已经建立了一个例子来研究这些。使用以下代码时,我得到的用户结果加倍为:

  • -Id 用户名
  • -1 约尔
  • -2 达达
  • -1 约尔
  • -2 达达

--- 我的代码 ---

// I think this tastypie adjustment is not related with the problem but i want you to see the //whole code

window.TastypieModel = Backbone.Model.extend({
        base_url: function() {
          var temp_url = Backbone.Model.prototype.url.call(this);
          return (temp_url.charAt(temp_url.length - 1) == '/' ? temp_url : temp_url+'/');
        },

        url: function() {
          return this.base_url();
        }
    });

    window.TastypieCollection = Backbone.Collection.extend({
        parse: function(response) {
            this.recent_meta = response.meta || {};
            return response.objects || response;
        }
});


(function($){



// MODELS-COLLECTIOS
  //USERS
  var User = TastypieModel.extend({
      url: USERS_API
  });

  var Users = TastypieCollection.extend({
      model: User,
      url:USERS_API
  });



//VIEWS
  var UsersView = Backbone.View.extend({
    render: function(){
         // template with ICanHaz.js (ich)
        this.el = ich.userRowTpl(this.model.toJSON());
        return this;
       }
  });





//main app
  var AppView = Backbone.View.extend({
      tagName: 'tbody',
      initialize: function() {
          this.users = new Users();
          this.users.bind('all', this.render, this);
          this.users.fetch();
      },

      render: function () {

          // template with ICanHaz.js (ich)
          this.users.each(function (user) {
             $(this.el).append(new UsersView({model: user}).render().el);
          }, this);



          return this;
      }
  });

  var app = new AppView();
  $('#app').append(app.render().el);






})(jQuery);
4

1 回答 1

2

我会说您正在触发两次渲染,因为您的视图正在侦听所有事件,请尝试绑定它只是为了重置并查看它是如何进行的:

initialize: function() {
  this.users = new Users();
  this.users.bind('reset', this.render, this);
  this.users.fetch();
},
于 2013-03-11T14:35:47.663 回答