Given a web application based PHP or ruby in its backend, and programmed on JavaScript in the frontend, sometimes we need to define values in both sides to process data that communicates between the frontend and backend through APIs.
In JavaScript would be something like this:
var options = {
OPT_A : 1,
OPT_B : 2,
OPT_C : 3
};
switch(data.type)
{
case options.OPT_A:
/* */
break;
case options.OPT_B:
/* */
break;
case options.OPT_C:
/* */
break;
}
And in php something like this:
const OPT_A = 1;
const OPT_B = 2;
const OPT_C = 3;
The problem is that you have to define them twice; once in each language. This could lead to errors/inconsistencies.
The only way I've thought is defining the JavaScript part from the server, but I don't like the idea of JS code being written by PHP/Ruby code.
Is there any way to this without code duplication?