Backbone.js 如何将参数传递到主干木偶复合视图模板

Backbone.js 如何将参数传递到主干木偶复合视图模板,backbone.js,marionette,Backbone.js,Marionette,有没有办法将参数输入到木偶复合视图模板中?我认为初始化视图时使用的任何参数都可以在模板中使用,但它似乎不起作用 Views.myView = Marionette.CompositeView.extend({ template: '#myView', otherstuff... }); var collection = new App.Collection(); App.main.show(new Views.myView({ collection: collection,

有没有办法将参数输入到木偶复合视图模板中?我认为初始化视图时使用的任何参数都可以在模板中使用,但它似乎不起作用

Views.myView = Marionette.CompositeView.extend({
  template: '#myView',
  otherstuff...
});


var collection = new App.Collection(); 
App.main.show(new Views.myView({
  collection: collection,
  isMine: true
}));
模板:

<%= isMine %> 


当模板呈现时,
isMine
是未定义的:

在freenode聊天室从brian mann那里得到了一些帮助来解决这个问题。我将该值传递给视图,但我需要通过重写
serializeData
方法将其作为属性发送给实际模板

我还做了一个检查,将默认值设置为true,这样如果我不想,就不必传入值

Views.myView = Marionette.CompositeView.extend({
  template: '#myView',
  serializeData: function() {
      var viewData = {};
      viewData.isMine = this.options.isMine === undefined ? true : this.options.isMine;
      return viewData;
    },
  otherstuff...
});

您可以设置视图“model:{isMine:true}”的model属性。

您可以使用templateHelpers函数来实现此目的。例如,我有一个在渲染时填充不同区域的布局

onRender: function () {
            var contactInfo = this.model.get('contactInfo');

            this.contactInfoRegion.show(new ContactInfoView(
                {
                    model: contactInfo,
                    travelerNumber: this.travelerNumber,
                    numberOfTravelers: this.numberOfTravelers
                }
            ));
}

var ContactInfoView = Backbone.Marionette.ItemView.extend({
        model: ContactInfoModel,
        template: Backbone.Marionette.TemplateCache.get(contactInfoTemplate),
        templateHelpers:function(){

            return {
                numberOfTravelers: this.options.numberOfTravelers,
                travelerNumber: this.options.travelerNumber
            }
        }
    });

+1这是正确的方法。如果有人想知道的话,你也可以直接将一个对象传递给templateHelpers,而不是一个函数:)尽管如果你确实传递了一个对象,你将无法直接访问“this”关键字。(抱歉,它不允许我编辑我的评论!)