patternjavascriptMinor
Creating bootstrap modals that can run JavaScript in MVC5
Viewed 0 times
cancreatingbootstrapjavascriptmvc5thatmodalsrun
Problem
I have an MVC5 & C# application using the Razor engine.
In my application I have a view, called "Index.cshtml". In this view I want to present a bootstrap modal to the user.
When presented to the user, this modal will have buttons that should be capable of running JS functions and methods using jQuery.
I have achieved this in the past using a dynamic method.
This means that I have a Main view (
Package List
$(".btn.btn-default.editPackage").click(function () {
$.ajax({
url: $(this).data('url'),
type: 'GET',
cache: false,
success: function (result) {
$('#editModal').html(result).find('.modal').modal({
show: true
});
}
});
return false; //prevent browser defualt behavior
});
Edit Package MyPackage
Package Name:
Close
Save changes
$("#savePackageChangesBtn").click(function() {
$('.modal').modal({
show: true
});
return false; //prevent browser defualt behavior
});
});
`
However, after discussion with the community (see this answer), we concluded that was a poor design pattern.
Therefore, I must ask, what is the easiest way of achieving this objective?
In my application I have a view, called "Index.cshtml". In this view I want to present a bootstrap modal to the user.
When presented to the user, this modal will have buttons that should be capable of running JS functions and methods using jQuery.
I have achieved this in the past using a dynamic method.
This means that I have a Main view (
Index.cshtml), that contains an empty element (`):
Package List
This empty tag is then filled via an Ajax request to the server:
//Show Edit Package modal$(".btn.btn-default.editPackage").click(function () {
$.ajax({
url: $(this).data('url'),
type: 'GET',
cache: false,
success: function (result) {
$('#editModal').html(result).find('.modal').modal({
show: true
});
}
});
return false; //prevent browser defualt behavior
});
This Ajax request returns the Modal that I want to show:
Edit Package MyPackage
Package Name:
Close
Save changes
And then the user can have fun clicking in the modal's buttons and seeing how things change. This is achieved via jQuery linked to the modals save button:
$(document).ready(function() {$("#savePackageChangesBtn").click(function() {
$('.modal').modal({
show: true
});
return false; //prevent browser defualt behavior
});
});
`
However, after discussion with the community (see this answer), we concluded that was a poor design pattern.
Therefore, I must ask, what is the easiest way of achieving this objective?
Solution
Simply put, you are using AJAX to return markup when you should be using AJAX to fetch data. If you are doing a per-item fetch using AJAX, you should be fetching more package detail information and returning it in an object, not as a munged HTML string. If you already have all the data you need when the page is loaded, then you should not be making additional AJAX calls, you should be doing all the work in Javascript.
I think Vsevolod Goloviznin made a good suggestion, to render the modal on the page once. That's good because it does not make the server have to generate a bunch of crap, but if you use straight-up jQuery you end up writing a bunch of messy procedural code to update the markup and event handlers (destroy and reassign) during the selection callback of your packages table.
Client-side templating is your friend. Here's an article that seems to be right up your alley:
http://www.smashingmagazine.com/2012/12/05/client-side-templating/
I think Vsevolod Goloviznin made a good suggestion, to render the modal on the page once. That's good because it does not make the server have to generate a bunch of crap, but if you use straight-up jQuery you end up writing a bunch of messy procedural code to update the markup and event handlers (destroy and reassign) during the selection callback of your packages table.
Client-side templating is your friend. Here's an article that seems to be right up your alley:
http://www.smashingmagazine.com/2012/12/05/client-side-templating/
Context
StackExchange Code Review Q#71770, answer score: 2
Revisions (0)
No revisions yet.