0

Hello and happy new year everybody!

I have a practical problem: On my GUI I ask the user to specify a new document name with prompt method when he clicks on "Save As New button"

    $("#btnSaveNew").on( "click", function() {
        var newScenarioName = prompt("Please enter new scenario name", "scenarioX");

    $.ajax({
                 url : 'http://' + document.domain + ':' + location.port + "/saveAsNew",
                 type : 'POST',
                 data : JSON.stringify({'data':data,'newName': newScenarioName}),
                 contentType: 'application/json;charset=UTF-8',
                 success: function (result) {
                            loadScenario();
                    },
            });

My python-flask part ask a cloudant DB to send the file ID matching the specified name, if the name doesn't exist it creates the doc according to the metaData content but if it exists, I have no solution to warn the user

newScenarioName = request.json["newName"]

file_id = getscenario(newScenarioName)
my_doc = my_db[file_id]

metaData = {
    'name': newScenarioName,
    'scenario': data
}

my_doc = my_db.create_document(metaData)

So I'd like to implement something like that

newScenarioName = request.json["newName"]

file_id = getscenario(newScenarioName)
my_doc = my_db[file_id]


if my_doc['name'].exist():
    scenario_exist = True

    ******************
    ** missing part **
    ******************

else:
    metaData = {
        'name': newScenarioName,
        'scenario': data
    }

    my_doc = my_db.create_document(metaData)

where the ** missing part ** includes a send instruction to the .js part that modify a var that I can add so i can make a condition such as

        var newScenarioName = prompt("Please enter new scenario name", "scenarioX");
        if (newScenarioName === null || scenario_exist === True) {
            newScenarioName = prompt("Please enter new scenario name, previous one was null or already used", "scenarioX");
            }

and abort the new scenario creation!

I saw a possible solution on: How to pass variable from python to javascript making a jQuery call, but the thing is, I didn't code the HTML/web-js part so I'm kinda lost in the jQuery possibilities (and with the very technical, not web-noob-friendly manual)

So is there a way on python side to make a method like request.json['key'] but that goes other way (post.json['key'] ? :p) Or maybe call the value .js side with an jQuery/Ajax method?

Thank you for your patience and for reading this issue and sorry for the grammar fault

EDIT: I think the only way to do so is to use websockets, the use of templates with Jinja seems a bit technically too much for me and it's more a bypass than a solution

Community
  • 1
  • 1
Manu
  • 352
  • 1
  • 4
  • 14
  • [Flask-Socketio](https://flask-socketio.readthedocs.io/en/latest/) might be what you're looking for. – MrLeeh Jan 10 '17 at 17:14

1 Answers1

0

Ok I just bypass my problem by redefining my ajax method, it calls back the function in case of error

    function saveNewHandler() {
        var newScenarioName = prompt("file name");
        $.ajax({
                 url : 'http://' + document.domain + ':' + location.port + "/saveAsNew",
                 type : 'POST',
                 data : JSON.stringify({'path':scenarioFile, 'data':data, 'name': scenarioName, 'newName': newScenarioName}),
                 contentType: 'application/json;charset=UTF-8',
                 success: function (result) {
                            loadScenario();
                    }
            })
            .error(function(xhr, status, error) {
                console.log(xhr.responseText);
                saveNewHandler(); // ask for new file name 
            });
        }
Manu
  • 352
  • 1
  • 4
  • 14