0

Has T4MVC is not extensible to external JavaScript file, I would like to find the best way for manage "Magic Strings" like a Controller/Action value in a JQuery .load() method.

I want to minimize the risk of the change of an Action to provoque an error in the application. I know that I can compile the Views with Visual Studio to find client side errors, but not in JS external files.

I found some solutions like include a View as a javascript file type so I can use T4MVC and detect Views compilaion errors, but this approach makes a server unnecessary request and if it was JS it would be cached at the client side.

I'm thinking about a constant JS file, or in the limit, a constant View file with T4MVC params method.

Any better ideia?

I leave here a sample:

<script type="text/javascript">
$(function () {
    $('#Category_Id')
        .cascade(
        {
            url: '@Url.Action(MVC.Ad.ListCategoryTypeByCategory())',
            paramName: '@MVC.Ad.ListCategoryTypeByCategoryParams.categoryId',
            firstOption: '@HeelpResources.DropdownlistCategoryTypeFirstRecord',
            childSelect: $('#CategoryType_Id')
        })
        .cascade(
        {
            url: '@Url.Action(MVC.Ad.ListMakeByCategory())',
            paramName: '@MVC.Ad.ListMakeByCategoryParams.categoryId',
            firstOption: '@HeelpResources.DropdownlistMakeFirstRecord',
            childSelect: $('#Make_Id')
        });

    $('#Make_Id').cascade({
        url: '@Url.Action(MVC.Ad.ListModelByMake())',
        paramName: '@MVC.Ad.ListModelByMakeParams.makeId',
        firstOption: '@HeelpResources.DropdownlistModelFirstRecord',
        childSelect: $('#Model_Id')
    });
});
</script>

How can I avoid "Magic Strings" if I want to put this code in a JS external file?

Thanks.

Patrick
  • 2,995
  • 14
  • 64
  • 125

3 Answers3

0

I know you already tried that, but if you want to use T4MVC I would think that returning JS from a server side .cshtml file is probably your best bet. Note that doing that does not implied that it won't be cached on the client side (if you get the headers right). In fact, from the point of view of the client, returning a pure JS file and returning one that comes from a .cshtml is not really distinguishable. Either way, it gets JS back.

David Ebbo
  • 42,443
  • 8
  • 103
  • 117
0

I think the method you use is fine, it's what I use. They aren't really magic strings because you are using route based Url generation, and you never have to worry about the Url's in the javascript becoming outdated.

Another approach could be to use a controller action that returns a JsonResult:

    public JsonResult GetConcentrationsMap() {
        var magicStrings = _magicStringsService.GetAll().Select(c => new { Name= c.Name, Url = c.Url });

        return Json(new { urls = magicStrings }, JsonRequestBehavior.AllowGet);
    }

This would get cached on the client, but as David Ebbo said, you can achieve that with .cshtml too. I think it's more trouble because you still have to manage telling your javascript code the path to the .js or json resource that has the magic strings.

Giscard Biamby
  • 4,569
  • 1
  • 22
  • 24
  • The sample example I gave only work inside a cshtml file, and is not possible in a JS external file. – Patrick Jan 02 '13 at 00:48
0

You could use JavaScriptModel ( http://jsm.codeplex.com ). If you need these "magic strings" globally, you should consider writing a filter and add them there.

Heres an example how to write a filter with JavaScriptModel:

http://jsm.codeplex.com/wikipage?title=Use%20JavaScriptModel%20in%20a%20global%20filter&referringTitle=Documentation

acuntex
  • 147
  • 13