Using Marionette.ItemView for views without models

2019-03-13 05:51发布

Is it conventional to use Marionette.ItemView for view classes that do not have a specific model property associated with them?

As Marionette.View is not meant to be used directly, it seems like an ItemView makes sense as a view class with convenient defaults and bindings.

Or, should one just resort to using Backbone.View? If so, is there a way to hook Backbone.View into Marionette's evented and garbage-collected architecture?

Thank you for clarification!

2条回答
混吃等死
2楼-- · 2019-03-13 06:26

I just found out you can use a templateHelper for this - just chuck this in your ItemView declaration:

templateHelpers: function() {
    return {
        message: this.message,
        cssClass: this.cssClass
    }
}

And then in your template:

<script type="text/html" id="notice-template">
    <span class="<%= cssClass %>"><%= message %></span>
</script>

And then when you initialise the view:

var noticeView = new App.Views.Notice();
noticeView.message = "HELLO";
App.noticeRegion.show(noticeView);

I would be interested in your thoughts on this Derick?

查看更多
成全新的幸福
3楼-- · 2019-03-13 06:37

ItemView can be used without a model. I do this quite regularly.

If you need to specify data for an ItemView, but not have that data in a Backbone.Model, you need to override the serializeData method:


MyView = Marionette.ItemView.extend({
  serializeData: function(){
    return {
      my: "custom data"
    };
  }
});

the base Marionette.View isnt' meant to be used directly because it doesn't provide a render function on it's own. That doesn't mean you can't use it to create your own base view types, though. You could, for example, build a view type for your application that deals with rendering google maps or a third party widget or something else that doesn't need the general Backbone.Model based rendering that ItemView has in it.

查看更多
登录 后发表回答