0
4

1 回答 1

0

从文档:

模板view.template([数据])

虽然视图模板不是 Backbone 直接提供的功能,但在视图上定义模板函数通常是一个很好的约定。这样,在渲染视图时,您可以方便地访问实例数据。例如,使用下划线模板:

var LibraryView = Backbone.View.extend({
    template: _.template(...)
});

所以 template 是一个自定义属性,我们可以用它来指向一个模板函数。定义这还不够,您必须通过将数据传递给模板函数并将结果附加HTML到视图元素来创建实际模板。

就像是:

 module.exports = View.extend({
  initialize: function(){
    this.render();
  },
  template: Handlebars.compile(template), // where template is the handle bars template
  events: {
   'click #btnSubmitModal': 'saveConsent',
   'click #consent-link' : 'openConsent'
  },
  render: function(){
    this.$el.append(this.template(data)); //where data is the data you wish to pass to handle bars template function
  },
  openConsent: function(event){
    event.preventDefault();
    console.log ("asaasagsgsgs");
    view = new modalView({model: this.model})
  }
 });

事件处理程序的范围是(委托给)视图的元素。一旦您将 附加HTML到视图的元素(如render上面示例的方法所示),事件将开始工作。

于 2015-10-23T10:19:20.107 回答