How do you maintain view state with Marionette JS?

It seems that Marionette is not designed to handle repeat views. What is an effective way to maintain view states (for all types of views) since they are always restored?

My first thought was to fix the state in the models, but I quickly parted with this idea, since many views can refer to the same model.

I tried reusing looks in several different models, but was not sure what I was doing right. In each case, I felt that the Marionette methods that I override could break things (now and / or later).

I found suggestions on how to do this with a clean Backbone, and also looked at Giraffe, who seems to be managing the condition quite efficiently, but I still have to find a solution for Marionette.

+4
source share
2 answers

Easy unsaved state system for receiving data into a template:

View.MyView = Marionette.ItemView.extend({
    serializeData: function() {
        return _.merge(this.model.toJSON(), this.options.state.toJSON());
    },
});

var view = new View.MyView({
    model: model,
    state: new Backbone.Model(),
});

With a slightly more advanced version, you can also use model events:

View.MyView = Marionette.ItemView.extend({
    initialize: function() {
        var self = this;

        this.options.state.on('change', function(event, data) {
            if (_.isFunction(self[event])) {
                self[event](data);
            }
        });
    },
});

Then in your controller:

view.options.state.trigger('change', 'functionName', 'someData');
+2
source

You can track view states using ViewModel.

Sam says a little about it: http://youtu.be/CTr-tTwRH3o

0
source

All Articles