Hi
What is this best way to call a function that returns a value or does something immediately after the save event. In my example below I wanted to save the data from the form and then I call a function to retrieve the newly created id. On its own the function works as expected. Put into the attachment code below I get value undefined.
Any help would be appreciated.
generatorForm.attachEvent("onButtonClick", function (btnName) {
// save or cancel
if (btnName == "cancel") {
callbacks.clearDashboard();
} else if (generatorForm.validate()) {
generatorForm.save();
console.log(LastAddedId("generators"));
}
});
my function:
[code]
LastAddedId = function(tble){
dhx.ajax().get("data/lastadded.php?id="+tble, function(text,xml){
var data = dhx.DataDriver.json.toObject(text,xml);
data = data.id["0"].id;
return data;
});
i’m pretty sure my problem is from not using callbacks correctly. In the demo I have a function that will return the last added ID from a table. I just wanted this available at the time of save on the form to perform other functions. I think the problem is that the value hasn’t been calculated yet and so I would need to wait. demo.zip (661 KB)
That worked great. I had no idea i could retrieve the ID this way. I know its documented, but, I guess you have so many great features that it is day for us to over look.
This has solved my problem for now as I don’t have to wait for a php query to retrieve the last ID.
I know it might be asking a lot, but, I’m new to javascript and am learning it because I like DHTMLX. I have several functions that Id like to implement which are similar to my old solution, how would I best structure my code to make it work? i.e. wait for a function then return the result to another.
there are lot of ways to call functions in ‘async world’, all depending on certain case. for example you may be interested in Promise, here is an article from google_search html5rocks.com/en/tutorials/es6/promises/