渲染骨干错误

时间:2013-05-04 15:01:04

标签: javascript backbone.js handlebars.js

我正在尝试渲染,但我不完全了解javascript并比较此错误: 未捕获的ReferenceError:未定义包装器。 我会在同一视图中呈现集合的提取结果。

     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;

      });    

1 个答案:

答案 0 :(得分: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();