Getting strange results with folderBrowserFS...

Started by Jingo, October 05, 2019, 07:26:22 PM

Previous topic - Next topic

Jingo

Hi Mario.. assuming I run the following code:

IMatch.folderBrowserFS({
     title: 'Please select a folder:'
}).then(function(response) {
     console.log(JSON.stringify(result,null,2));
});

I would expect the console.log to only show once the user selects a folder (or cancels) out of the dialog and the promise is returned to the software.  However, on my system, the console log fires while the File browser displays.  I am using more complicated code in my Gallery program and while it was working earlier today - the code no longer seems to return the promise.. so I figured I was try something very basic ... it also doesn't work as expected!  Thoughts??


Jingo

I actually had a syntax error in the first one.. but still getting some strange results after correcting the syntax... why would I get the promise when I have not selected a folder and hit OK/Cancel?


Jingo

#2
Ok.. I've narrowed down the reason for the issue... it looks like the $(document).ready function()  is getting called multiple times... and this is causing issues.

I modified the test code snippet to read:

console.log("123");
IMatch.folderBrowserFS({
     title: 'Please select a folder:'
}).then(function(response) {
     console.log(JSON.stringify(response,null,2));
});


This produces the following output and displays the file dialog box as expected.


123


HOWEVER...... a few seconds later... I get the "re-run" result of:

123
123
{
  "result": "failed",
  "resultMessage": "Another dialog is already open."
}


So... I don't understand why the $(document).ready function is running more than once.... how does one prevent it from triggering more than one time per load?

Here is the resulting code in the TESTER app that gets auto-generated in case it helps... but this is also an issue with some other apps I've written (they don't seem to cause an issue - but it would be good to prevent it from happening):


<!DOCTYPE html>
<html>
<head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title imt-translate>Imatch Code Tester App</title>
        <script src="/system/jquery/dist/jquery.min.js"></script>
        <link rel="stylesheet" href="/system/font-awesome-5/webfonts/css/all.min.css" />
        <link rel="stylesheet" href="/system/animate.css/animate.min.css" />
        <link rel="stylesheet" href="/system/bootstrap-4/css/bootstrap.min.css" />
        <link rel="stylesheet" href="/system/themes/bootstrap-4/slate/bootstrap.min.css" />
</head>

    <body>
        <div class="container">
            <h1 id="app-name">Imatch Code Tester App</h1>
            <p id="info"></p>
            <p>Your Code snippet will now run.... </p>
        </div>

    <!-- jQuery and Bootstrap -->
    <script src="/system/jquery/dist/jquery.min.js"></script>
    <script src="/system/bootstrap-4/js/bootstrap.bundle.min.js"></script>
    <script src="/system/imws/imwslib.js"></script>
    <script src="/system/imatch/imatchlib.js"></script>
    <script src="/system/imatch/imatch-modals.js"></script>

    <script>
            $(document).ready(function () {
                /// code that is to be tested in the app will append after these lines!

console.log("123");
IMatch.folderBrowserFS({
     title: 'Please select a folder:'
}).then(function(response) {
     console.log(JSON.stringify(response,null,2));
});});</script></body></html>

Mario

#3
Quote$(document).ready function is running more than once

Cannot happen, until you somehow force your browser to reload your page (form submit handler, onClick handler not calling event.preventDefault or not returning false etc.). Common problem.
Check the network tab of your browser to see how many times you reload the page or when the reload is triggered.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

Jingo

Quote from: Mario on October 06, 2019, 08:34:51 AM
Quote$(document).ready function is running more than once

Cannot happen, until you somehow force your browser to reload your page (form submit handler, onClick handler not calling event.preventDefault or not returning false etc.). Common problem.
Check the network tab of your browser to see how many times you reload the page or when the reload is triggered.

Well... something really strange is going on here.  If I load a TEST APP (shown below) and run the app DIRECTLY from app manager... it works every time (only runs once).  But, if I run the same code via the IMatch.appRun command - it fires the code within the app twice...  same code... just one called directly from app manager and the other called by IMatch.appRun.


<!DOCTYPE html>
<html>
<head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title imt-translate>Imatch Code Tester App</title>
        <script src="/system/jquery/dist/jquery.min.js"></script>
        <link rel="stylesheet" href="/system/font-awesome-5/webfonts/css/all.min.css" />
        <link rel="stylesheet" href="/system/animate.css/animate.min.css" />
        <link rel="stylesheet" href="/system/bootstrap-4/css/bootstrap.min.css" />
        <link rel="stylesheet" href="/system/themes/bootstrap-4/slate/bootstrap.min.css" />
        <script src="/system/bootstrap-4/js/bootstrap.bundle.min.js"></script>
<script src="/system/imws/imwslib.js"></script>
<script src="/system/imatch/imatchlib.js"></script>
<script src="/system/imatch/imatch-modals.js"></script>

<script>
   $(document).ready(function () {
          /// code that is to be tested in the app will append after these lines!

console.log("23");
IMatch.folderBrowserFS({
     title: 'Please select a folder:'
}).then(function(response) {
     console.log(JSON.stringify(response,null,2));
});});</script></head><body><h1 id="app-name">Imatch Code Tester App</h1><p id="info"></p><p>Your Code snippet will now run.... </p></body></html>



Code used to launch the app:

IMatch.appRun({
                        appid: '5E9966B3-3AC9-4DC9-8D78-E344B1144',
                        target: 'app-panel-4',
                        width: 600,
                        height: 800,
                    });

Mario

I don't understand.

appRun internally calls the App Manager and tells it to launch the specified app.
The same code is executed when you run an App from the App Manager or the App Panel directly.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook

Jingo

Quote from: Mario on October 06, 2019, 03:02:46 PM
I don't understand.

appRun internally calls the App Manager and tells it to launch the specified app.
The same code is executed when you run an App from the App Manager or the App Panel directly.

Yup... that's why I don't understand why the script is launching twice via appRun and only once via the App Manager.    I know you don't review or debug user created APPs, but this is my Code Tester application.  It simply takes an app "template", copies it to a TEMP app folder, adds the code from the text box and some default closing app code.. and then launches the app to run the code.  It works like a charm... but I notice the script code gets launched twice.  Running the same TEMP app directly via App Manager runs only once.  Very strange....

Oh well... its a mystery for sure!  I fixed the original issue with my gallery code by changing to a $(window).bind ("Load") function... and removing the <DIV form>...

Mario

Cannot reproduce.

1. Add a console.log to the $(document).ready(function () { in the "Info app".


            $(document).ready(function () {
                console.log('Running Info App.');


2. In a test app, add a call to IMatch.appRun and link it to a button.


                IMatch.appRun({
                    appid: 'photools.com.IMatch.Info',
                    target: 'app-panel-4',
                });


3. Click button. Info app opens in panel 4 and one (!) console log is displayed.

4. Add a breakpoint to the App Manager code that launches apps.

5. Press button again. The App Manager is called once (!) to launch the app.
-- Mario
IMatch Developer
Forum Administrator
http://www.photools.com  -  Contact & Support - Follow me on 𝕏 - Like photools.com on Facebook