Is there a way to re-render child view on any custom JQuery event in Backbone/Marionette?

256 Views Asked by At

I want to re-render my child view that is being rendered in a composite view on $(window).resize() event which I have subscribed in onShow() of my composite view. Can we do do that? if yes, is there a preferred way? I want to something like this:

define([
 "app",
 "views/list-item",
], function(App, ListItem) {
   var List= App.CompositeView.extend({
     template: "list",
     childViewContainer: ".list-items",
     childView: ListItem,
     onShow: function() {
        $(window).on('resize', function() {
          //re-render child View(ListItem)
        });
     }
  });
 return List;
});
2

There are 2 best solutions below

0
Paraminder Singh Uppal On

You can implement 'modelEvents' in parent view. On change of 'modelEvents' call render method which will re-render the view. For example:

"modelEvents" :{ "change:updateView": "render" }

And toggle the 'updateView' model to call this event.

0
T J On

According to this PR CompositeView has a renderChildren method, depending n your version of marionette. If it is old it might be private _renderChildren.

Side note: having global selectors and events inside a view is bad practice. I'd put that logic in a separate script outside of the view.