2

I am working on an ASP.NET MVC3 app. I was reading about good Javascript practices in various places and decided it would be best if I externalized the javascript and jQuery calls that were sitting in a <script/> tag at the bottom of my views.

I put each view's Javascript in a separate file with a standardized naming convention so I could generate the <script/> tag to include the appropriate file in my layout view.

But it's on the verge of being too complicated to be maintainable, and I have run into one problem that breaks the whole scheme, namely the need to occasionally insert URLs into the javascript.

For example: if I'm doing a $.getJSON call, I need to provide a URL. I've been generating them on the server side, in the views, using the UrlHelper class, like so:

var _RegistrationSelectClassUrl = '@Url.Action("SelectClass", "Student", new { area = "Registration", id = Model.Person.PersonID })';

And this worked nicely ... up to the point where I externalized the javascript. No longer being part of views, calls to UrlHelper were just strings again.

So my question is this: if I stick with the externalized javascript, I need a way to insert or generate the appropriate URLs. Other than sticking a script section in the layout file that uses UrlHelper to define a constant for every URL I might want to use, I can't think of a good one.

Any suggestions?

2 Answers 2

2

External JS:

var namespace = {
    init: function (url) {
        this._RegistrationSelectClassUrl  = url;
    },
    _RegistrationSelectClassUrl: ''
};

View:

<script src="external/js.js" />
<script>
    namespace.init('@Url.Action("SelectClass", "Student", new { area = "Registration", id = Model.Person.PersonID })');
</script>  

This is a basic example.

Sign up to request clarification or add additional context in comments.

3 Comments

I'd also go with something like this.
@JoeTuskan I suppose I'm stuck with having some JS in the view, no matter what, if I'm going to use Url.Action. Thank you!
yeah, unfortunately. However, this allows you to minimize the amount at least. Good luck!
1

You can't. The moment the script leaves the parsed page it loses all access to the model. If you added this code into a PartialView, you would be able to parse things with the Helper classes, but since your link is dependent on the model, your scripts will have to be generated in the page itself.

A way to externalize it might be to have a mapped array of links that is populated by the model view itself. You could then add link text into this array mapping based on some key that would then be accessible by the external script after load. I would recommend not using the global variables for this and prototype an object for this purpose.

Some documentation for this: http://www.javascripttoolbox.com/bestpractices/#namespace

1 Comment

That link is great! And I appreciate your suggestions!.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.