Duplicate constants between Javascript and server code

In my project, I have some of the same variables as in my JavaScript code, as on my server (written in another language). I have comments that when one side changes, the other should change. This seems like a clumsy way to share these variables.

Is there a standard approach for exchanging variables between JavaScript scripts and the content of my website?

Some options I was thinking about:

  • Use the source file for common constants if you're lucky, and the syntax for both languages ​​is compatible.
  • Dynamically generate a constant file for one of the languages ​​based on the other, as part of the build process.
  • Create all JavaScript using the template language.
  • Put common inline constants in HTML, rather than put them in a JavaScript file.
+4
source share
2 answers

I assume you are using single page applications. You can change your server to be calm, and whenever something needs to be changed, your Javascript can update itself and report it.

Take a look at popular javascript frameworks, for example AngularJS, and see how single-page applications are developed. The first few tutorials should catch you.

No, you will not share variables directly. You will share what value contains your variables. When someone changes, you will inform your application about updating another.

, , , JSON.

{
    "users": [
        {
            "firstName": "John",
            "lastName": "Doe"
        },
        {
            "firstName": "Anna",
            "lastName": "Smith"
        },
        {
            "firstName": "Peter",
            "lastName": "Jones"
        }
    ]
}

javascript. , (, ), , , .

, , AngularJS .

+2

.

, API, /, JavaScript. API , .

, , JS, API/ .

0

Source: https://habr.com/ru/post/1537555/


All Articles