How to keep C# and Javascript Code in sync? -


i should think problem come anyone, i'm interested in ways of making best use of technology keep code-base dry , in sync.

i'm developing asp.net mvc5 web application. have several variables need communicating client side. "pagetype", need tracked on client side using google analytics. intent may multifarious, simple application same purpose, illustration.

i have c# class allows me specify various pagetypes , switch between them switch-casing, say:

public static class pagetypes {     public const string home = "home";     public const string aboutus = "aboutus";     public const string contactus = "contactus"; } 

this useful when check dynamic values, like:

string pagetype = viewbag.pagetype; if(!string.isnullorwhitespace(pagetype)) {     switch(pagetype)     {         case pagetypes.home:             // here             break;         case pagetypes.aboutus:             // else here             break;         ... 

... or when doing comparisons. hope point. so, in razor view, can output var pagetype = '@viewbag.pagetype'; in page in inline script, , present var pagetype = 'home'; or such case, in inline javascript of page. now, did c# code, want able switch-case page type inside javascript.

// part dynamic inline script of razor view var pagetype = 'home';  // part logic can reside in external script  // part illustration, worry naming , accessibility later // in separate script , included, , referenced, thereby providing intellisense within visual studio var pagetypes = {     home: 'home',     aboutus: 'aboutus' };  // should able this, in external script switch(pagetype){     case pagetypes.home:         alert('home!');         break;     case pagetypes.aboutus:         alert('not home');         break;     default:         alert('nothing');         break; } 

it can seen if static class pagetypes of c# in sync var pagetypes in javascript, boon our developers, intellisense , predefined values, may avoid typos , incompatibilities of values.

what's best way achieve this, while gaining intellisense in both c# , javascript?

i prefer changes in c# affecting js rather reverse. switch statements need not converted, constant values have mentioned.

i think script in build process useful there tool/plugin/alternate way accomplish have mentioned?

if have understand problem is, can is:

  • set pagetype in variable inside html markup, using template engine (sorry, don't work .net, don't know how write it). in example, used data-* in body element

< body data-type="%some template engine syntax output page type%">

  • in js, retrieve data-type value (in example, used jquery)

    var type = $("body").data("type");

  • define functions, each 1 pagetype, inside key/object variable / object

    var pagetypefunctions = { home: function() {

    }, aboutus: function() {  }, xpto: function() {  } 

    }

  • invoke function

    pagetypefunctionstype;

with approach, won't have deal pagetypes variable. need 1 function each pagetype. of course can check validate if pagetypefunctions[type] exists.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -