Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the onActive event triggers before the view is active #80

Open
aarc opened this issue Jun 16, 2015 · 3 comments
Open

the onActive event triggers before the view is active #80

aarc opened this issue Jun 16, 2015 · 3 comments

Comments

@aarc
Copy link

aarc commented Jun 16, 2015

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
Copy link

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
Copy link

ffflabs commented Sep 26, 2017

@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
Copy link
Contributor

jimf commented Oct 3, 2017

+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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants