fabio
fabio

Reputation: 1365

Using external variables in Javascript

In my views.py I had things like that:

...
if mymodel.name == 'Myname1':
    #do something
elif mymodel.name == 'Myname2':
    #do something else
...

but I didn't like it because if Myname change I should search all my code to correct it, so I create a file where I keep all this words:

hardcoded_words.py:

myname1='Myname1'
myname1='Myname2'
myname1='Myname3'
...

and my views.py become:

from myapp import hardcoded_words
...
if mymodel.name==hardcoded_words.myname1:
    #do something
elif mymodel.name==hardcoded_words.myname2:
    #do something else
...

If Myname1 changes I need to correct just one file:

hardcoded_words.py:

...
myname1='Myname1_b'
...

Maybe there's some better way (fell free to tell) but the problem is with Javascript. There's a way to do something like that?

My javascript.js:

function myfunction1(myvariable1, myvariable2) {
    switch (myvariable1) {
        case 'Myname1':
            //do something
        break;
        case 'Myname2':
            //do something else
        break;
        ...

Thank you for your help

Upvotes: 0

Views: 50

Answers (3)

Paul
Paul

Reputation: 6737

I would say that it's better to refactor your hardcoded words into dict like this:

name_mappings = {'myname1'='Myname1'
                 'myname1'='Myname2'
                 'myname1'='Myname3'}

To be able to import it into the simple Django view you will access via AJAX via url like /api/get_name_mappings/.

Because in this case you will have only one 'vocabulary' and you don't need to maintain it on the backend and on the frontend. And also if you want to add some more complex logic it would be also better to do it on the one side - server side.

The only case when this approach could be inconvenient - when you want to make unit tests for your javascript. It that case you will need to make some js-mocks.

But I agree with @Sayse that this way is better and clearer anyway.

Upvotes: 1

Sayse
Sayse

Reputation: 43320

If you really need to have the same list in javascript then I'd recommend creating a view you can call from an AJAX request that will just return the python dictionary that stores all of these values. This way there isn't any duplication and places where you'd need to update the same thing twice (DRY).

Then simply, in the areas where your logic will need to use this, make sure that this view is called before you ever use the values.

Ideally though, you may want to look into the logic involved here and see if there really is a requirement for "magic" strings.

Upvotes: 2

Kunso Solutions
Kunso Solutions

Reputation: 7630

You can do exactly the same in JS, just create a separate file with Object which will host your key, values.

Depend on the which specification of JS you are using you can do:

ES6:

export const hardcoded_words = {
    myname1: 'Myname1',
    myname1: 'Myname2',
    myname1: 'Myname3'
}

ES5:

window.hardcoded_words = {
    myname1: 'Myname1',
    myname1: 'Myname2',
    myname1: 'Myname3'
}

Upvotes: 1

Related Questions