What is a good strategy for dealing with large javascript code in asp.net MVC projects? -
I am trying to understand the best way to manage my javascript code for my MVC project. Due to the workflow of some page pages on my site, the user interface is very heavy with JavaScript.
To simplify the development and debugging, I split all of my Javascript into four .js files, Initializations.js
, which allows any type of jquery control and field To control the initials, DataRetrieval.js
which contains all functions for receiving and displaying data through AJAX, DataModification.js
, which can be used through Ajax Send data modifications to server There are all functions for the user, and there are utility functions to help in utilities.js
.
Structuring it in this way is not just the fact that I know which file has a specific function based on the function, but it keeps my javascript files too small. It also helps in debugging because I can see the script directly from the debugger of Chrome, that file can choose and I need to debug the function easily. The one I am reading this also helps because most browsers You can download many .js files with this and thus can load the page faster, making it easy to reduce in later times.
The issue I am seeing is that I can not embed them into the C # code by using .js files, because they do not have ideas. This means that all my URLs should have a magic string for them (so if I change the route then I have to change all the URLs manually) and I can not use the constants, which I set in C # Has been done so that the board can be manageable in every way. If I make a continuous change, such as an integer that means that a project is unpublished, then I have to search for all the references in my JavaScript files and have to change it manually, which may cause bugs).
There is now a possibility that I was thinking about changing my javascript files into partial sequences. However, it is still very difficult to debug a page in a web browser because the source code of the page will be absorbed by JavaScript. It also makes it difficult to reduce JavaScript at a later time.
How can I use other strategies to manage my javascript and to maintain it with the site's C # / MVC parts? it happens?
The web application part of my site is still very much to develop, so there is far more javascript to write, and before coming out of hand I want to come up with a strategy beforehand There are only.
Keep it in your JS structured manner, but instead of using it, give it important content Magic strings.
Write an HTML Helper to pass the minimum data to JS and use it in your site. Master, e.g. Here is a very simple example.
Public MvcHtmlString ScriptData (This HtmlHelper Helper) {var scriptData = "window.Routing.ImportantRoute = '" + Url.Action ("ActionName", "ControllerName") + "'; '; // etc. Back to MvcHtmlString.Create ("script type = 'text / javascript' & gt;" + scriptData + "& lt; / script & gt;");}
< P> This technique is good because it limits the amount you passed.
Comments
Post a Comment