如何将参数传递给主干牵线木偶复合视图模板

fly*_*ish 12 backbone.js marionette

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

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


var collection = new App.Collection(); 
App.main.show(new Views.myView({
  collection: collection,
  isMine: true
}));
Run Code Online (Sandbox Code Playgroud)

模板:

<%= isMine %> 
Run Code Online (Sandbox Code Playgroud)

当渲染模板时isMine未定义:

The*_*ica 17

您可以使用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
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)


fly*_*ish 6

得到了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...
});
Run Code Online (Sandbox Code Playgroud)