0

I'm trying to define a RequireJS module that consists only of a script tag - how do I do this? Although it may not be pertinent, the reason I'm trying to do this is I want to share a constants file between C# MVC 5 and JS - see the question and accepted answer here.

Here's what I have so far:

define('ClientServerSharedConstants', function () {
    <script type = "text/javascript" src="@Url.Action("Constants")"></script>
});

How do I do this?

Community
  • 1
  • 1
kibowki
  • 4,206
  • 16
  • 48
  • 74
  • You could sniff the route for the constants module file and create it in code upon first request. You wouldn't have to have the shell and try to razor it. Just dump the cached string contents of the file to the request stream, or via a FileResult, or whatevs. –  Jul 10 '15 at 15:07

1 Answers1

0

Looks like you want something like this:

define('ClientServerSharedConstants', function () {
    @Url.Partial("Constants")
});

In your example (assuming it worked, which I don't think it does) you would leave to the client (browser) to download the js file.

What I propose is to have a partial view that will be render as javascript in the resulting HTML eliminating the need of the client to download the file.

tucaz
  • 6,524
  • 6
  • 37
  • 60
  • Getting `Uncaught SyntaxError: Unexpected token ILLEGAL` on line 2 of the code you posted when I try that. You were correct in assuming the example I included was incorrect - I mostly included it to convey the idea of what I was going for. Any ideas? – kibowki Jul 10 '15 at 15:10