awkward / backbone.modal

A plugin for Backbone.js that simplifies creating modals for your application.
http://awkward.github.io/backbone.modal/
Other
362 stars 71 forks source link

the onActive event triggers before the view is active #80

Open aarc opened 9 years ago

aarc commented 9 years ago

I'm trying to validate a form within a view when it is displayed. But the onActive event is triggered before the view has been attached to the page.

Ideally I'd like to do something like this.

'click #step1': {
    view: _.template($('.template-step1').html()),
    onActive: function() {
        this.$el.find('.js-register-form').validate();
    }
}

But I've had to do something like this.

'click #step1': {
    view: function() {
        var $view = _.template($('.template-step1').html());
        $view.find('.js-register-form').validate();

        return $view;
    }
}
tylerjharden commented 9 years ago

I'm running into a similar issue with elements I am trying to use Selectize on, when switching tab views or wizard views they go back to unstyled selects because the jQuery call is running before the elements are rendered.

ffflabs commented 6 years ago

@aarc I've just had to deal with this issue.

My workaround, adapted to your case, would be

'click #step1': {
    view: _.template($('.template-step1').html()),
    onActive: function(modal) {
        modal.$(modal.viewContainerEl).html(modal.currentView);
        modal.$el.find('.js-register-form').validate();
    }
}

because onActive receives the instance of the parent Backbone.Modal.

jimf commented 6 years ago

+1, I think this makes sense. I need to think a bit more as to whether it's safe enough to move when the callback is called vs adding a second callback, but I can't think of a great reason to keep it where it is.

FWIW, you could also do the validation inside a setTimeout, but that sucks too.