0

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

2
  • Hard to say without knowing the logic involved... the amount of hardcoding this could need seems like it may have a logic flaw to me... its not very often you should need to rely on a string Commented Oct 10, 2016 at 8:26
  • For example Myname1-2-3 could be options for the user (Good, Average, Bad) to choose by. Tomorrow maybe I'll want to add options and so change the names or what before happened for Good will happen for Excellent. I don't want to use the id because (maybe I'm wrong) I think it will change more often. Commented Oct 10, 2016 at 8:44

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

but you say is better just leave that words and eventually change them when (if) I need to?
I don't know if I made my situation clear enough but basically I have to call some model istances and I don't trust id and fields.
@fabio - The way I understand your question is where to store a list of constants that are needed in both python and javascript, and essentially I'm saying just keep them in python and retrieve the python list in javascript when you need them
1

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'
}

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.