Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering a closed Marionette view

Shouldn't a closed Marionette view re-delegate the defined events (events, modelEvents, CollectionEvents) when rendering again?

It seems as if I have to manually call delegateEvents after closing and re-rendering a view. Otherwise the view won't work as expected.

http://jsfiddle.net/4DCeY/

var app = new Marionette.Application();

app.addRegions({
    main: '.main'
});

var MyView = Marionette.ItemView.extend({

    template: _.template('Hi, I\'m a view! Foo is: <%= foo %>'),

    modelEvents: {
        'change': 'onChange'
    },

    onChange: function() {
        alert('change!');
    }
});


var Model = Backbone.Model.extend({});


app.addInitializer(function() {
    var m = new Model({foo: 'bar'});
    var myView = new MyView({
        model: m
    });

    app.main.show(myView);
    myView.close();
    app.main.show(myView);

    m.set({foo: 'baz'});

});

$(document).ready(function(){
    app.start();
});
like image 824
Malte We Avatar asked Jul 29 '13 09:07

Malte We


1 Answers

If I understand your question right, there are multiple open github issues about this.

For example:

https://github.com/marionettejs/backbone.marionette/pull/654 https://github.com/marionettejs/backbone.marionette/issues/622

Last time I checked, Derick (the creator of Marionette) didn't feel like reusing closed views should be something regions should do.

So you could

  1. simply create a new view and show that one
  2. manually call delegateEvents - but there was an issue with multiple event bindings that I can't remember right now, so be careful about that one (not at work right now, so can't take a peek at the code, sorry)
  3. write your own region manager
  4. or wait and see if Derick will merge one of the pull requests
like image 153
django Avatar answered Oct 05 '22 22:10

django