0

我正在尝试渲染,但我不完全了解 javascript 并比较此错误:Uncaught ReferenceError: wrapper is not defined。我会在同一个视图中呈现集合获取的结果。

     var HomeView = Backbone.View.extend({

 template: Handlebars.compile(template),

 events: {


  },

  initialize: function() {

      console.log("inhomeview");

      var amici = new Usercollection();
      amici.fetch({
      success: function() {
      amici.each(function(object) {

      console.log(object.toJSON());
      var wrapper=object.toJSON();



    });
   },
    error: function(amici, error) {
    // The collection could not be retrieved.
   }
       }); 

      this.render();

  },

   render: function() {

      var context=wrapper;
      var html =this.template(context);


      this.$el.html(html);


     return this;
     }



     });

    return HomeView;

      });    
4

1 回答 1

0

也许您想做类似的事情(请阅读下面代码中的注释):

var HomeView = Backbone.View.extend({

  template: Handlebars.compile(template),

  initialize: function() {
    // its better to pass collection into view and listen to its 'reset' event
    this.collection.on('reset', this.render, this)
  },

  render: function() {
    // convert collection to json and pass to template as "users"
    var html = this.template({users: this.collection.toJSON()});
    this.$el.html(html);
    return this;
  }

});

// This is how you should be using it later in your code:
// create collection and pass it in home view
var users = new Usercollection(),
    homeView = new HomeView({collection: users,
                             el: '#pagina'});
// fetch collection, this will trigger 'reset' event
// so your view will render itself
users.fetch();
于 2013-05-04T16:43:57.893 回答