javascript - Passing data from my razor view to my js file -


i'm searching best way pass data razor view js file. example, lets have jquery dialog configured in js file. buttons text on dialog, localize (through resource files fr/nl/uk). translations available @userresource.buttondelete + @userresource.buttoncancel

below different solutions see:

  1. using nice razorjs nuget package allows razor code inside javascript file. works pretty well. question is: is bad practice compile js files in order use razor syntax inside scripts?

  2. declaring global variables in js script file , assign value view this:

in view:

<script>         var labelbuttondelete = @userresource.buttondelete; </script> 

in js file:

alert('the text button ' + labelbuttondelete); 

what best way pass data razor js file? have alternative?

thanks anyway.

i've been using second approach time without issues. difference i'm using singleton in js file avoid polluting global javascript namespace.

but if doing more serious client side stuff, javascript code follow more object oriented structure, , there automatically single initialization/constructor path can pass localized values.

that razorjs looks nice, i'm not sure if i'm comfortable mixing javascript razor. might small project, can see becoming messy if have lots of javascript files.

after all, still consider resources/localization code related view. javascript should implement functionality in opinion.


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -