Allow modal options to be changed after instantiation #5514

Closed
wants to merge 3 commits into
from

Conversation

Projects
None yet
5 participants

jtward commented Oct 14, 2012

When you call modal on an element, bootstrap will currently always use the options passed in the first call. This pull request allows subsequent calls to modal with different options to change the behaviour of the modal.

Maybe they caches the result for repeated opening modal.
This code can be used to disable cache forcibly.

$('#html_modal').on('hidden',function(){
    $(this).removeData('modal');
    $('#html_modal .modal_label').text('default_label');
    $('#html_modal .modal_body').html('default_body');
});

jtward commented Oct 15, 2012

@cielavenir Yes, you could do this yourself, although I haven't tested your code. I think it would be useful for this to be in bootstrap, but It's not up to me.

One difference between your code and my proposed change is that in your code all previous options are wiped. I'd like the new options to extend the previous options so that you don't have to respecify them, although I'm open to discussions on this.

Unfortunately, there's a nasty bug I've just spotted in that the default options always override previous options, so I'm closing this request until I get that fixed.

@jtward jtward closed this Oct 15, 2012

jtward commented Oct 15, 2012

Fixed the bug; the delete avoids cyclic references in data, and the semicolon is to keep jshint happy.

A perhaps more elegant solution would be to change the line

, options = $.extend(..., $this.data(), ...)

to something like

, options = $.extend(..., _.pluck($this.data(), 'backdrop', 'keyboard', 'show', 'remote'), ...)

to avoid storing unused data attributes in options. We would need _.pluck, though.

@jtward jtward reopened this Oct 15, 2012

Owner

fat commented Oct 18, 2012

you can change the modal options at any time by just accessing the raw class obj.

  $myElement.data('modal').options = { … }

@fat fat closed this Oct 18, 2012

I'd like to see this issue re-opened and addressed. Allowing modals to change after instantiation would allow for multiple buttons to reuse one hidden div (see twitter#5161 (comment) and http://stackoverflow.com/questions/14045515/how-can-i-reuse-one-bootstrap-modal-div).

troex commented Jan 23, 2013

$(this).removeData('modal'); on hidden did the trick to reuse the dialog, this is not necessary to be included in the code but should be documented somewhere. At least document that modal is not reusable by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment