Author Topic: What is actually required to get or set scriptsettings in a simple app?  (Read 4037 times)

JohnZeman

  • Global Moderator
  • *****
  • Posts: 1400
  • I'm too damn old to act my age.
I've successfully used scriptsettings many times in normal IMatch scripts but I haven't been able to get even the simplest example to work in an HTM app.  Is the IMatch library required?  jQuery?  If so, they aren't working for me which obviously means I'm not using them right..

Mario posted the below code as an example before I left on vacation last month but now that I'm trying it all I get are JavaScript errors.

Code: [Select]
// Set a setting
IMatchLib.getScriptSettings().Set("name of your setting","value of your setting");
...
// Get a setting
IMatchLib.getScriptSettings().Get("name of your setting");

I keep studying the Report app, the Geonames app, and the VarToy app to learn how to do this but I'm obviously missing something.  :'(

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 28570
Almost all sample apps I ship with IMatch use the ScriptSettings.

Dos the ReportApp remember it's settings on your machine (e.g. when you change it to use other variables)? If so, the code and the ScriptSettings work in principle. You should be able to copy/paste the code into your script.

Do you include the IMatch JavaScript library in your App?

<script src="../../lib/imatch_app.js" type="text/javascript"></script>



JohnZeman

  • Global Moderator
  • *****
  • Posts: 1400
  • I'm too damn old to act my age.
Dos the ReportApp remember it's settings on your machine (e.g. when you change it to use other variables)? If so, the code and the ScriptSettings work in principle. You should be able to copy/paste the code into your script.

Yes, it remembers any changes I make to it.

Do you include the IMatch JavaScript library in your App?

Code: [Select]
<script src="../../lib/imatch_app.js" type="text/javascript"></script>

On my machine the actual relative path to the imatch_app.js file is

Code: [Select]
<script type="text/javascript" src="../lib/imatch_app.js"></script>
but that returns a JavaScript error when I use it.  If I copy and paste the path you posted then I still get a JavaScript error that says IMatch.Lib is undefined.

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 28570
Sorry, I copied this path from the RepotApp, which lives in a separate sub-folder.

Please find attached a BasicApp which does nothing. It's just an empty hull.
I've  written this small app as a starting point and will ship it with IMatch 5 from now on.

1. Extract the contents of the attached ZIP file to a folder in the "apppanel\user" folder.
2. Open the BasicApp.XML file and change the name of the App to your liking.
3. Open BasicApp.HTML to add your code.

This app uses a resource file and displays a message. If this message is shown, you know that the resource file and the IMatch scripting work. And then ScriptSettings will also work.




[attachment deleted by admin]

JohnZeman

  • Global Moderator
  • *****
  • Posts: 1400
  • I'm too damn old to act my age.
Thanks Mario, I really appreciate this.  ;D
I downloaded your basic app and it looks like it'll be much easier for me to work with although I did have to correct the path to ../lib/imatch_app.js before it would work in the user folder.

Also I was finally able to successfully get and save settings in a copy of the report app so I know now it does work.  Hopefully I can start to figure this puzzle out now, but if I run into another brick wall you'll likely hear me whining again. lol

JohnZeman

  • Global Moderator
  • *****
  • Posts: 1400
  • I'm too damn old to act my age.
I'm making progress now Mario but I have one more question.  When I use your basicapp as a template things are working but none of the IMatch variables are being parsed (see attachment).  Do you know what I'm missing?


[attachment deleted by admin]

JohnZeman

  • Global Moderator
  • *****
  • Posts: 1400
  • I'm too damn old to act my age.
I've narrowed the variable parsing problem down to the app type defined in the XML file.  When it's set to APP, it doesn't parse variables, when I change the APP type to HTML it doesn't save settings.  Is this by design?

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 28570
Exactly.

This difference is explained in the help in more detail, but for HTML templates, IMatch loads the HTML file into memory, parses all variables, and then fills the result into the browser. This is convenient for simple HTML-based displays, but also limited in many aspects.

Apps use a mix of JavaScript and HTML. IMatch loads the HTML file into the browser, and the browser does the rest. No parsing takes place. This gives you the most flexibility and power.

If you want to parse variables, you have to call the Parse method explicitly. For example, if you have a HTML element with the id filename somewhere in your page:

Code: [Select]
<html>
<body>
<div id="filename"/>
</body>
</html>


You can do something like this in your render method:
Code: [Select]
var file = IMatchLib.focusedFile();
if (file) {
  document.getElementById("filename").innerHTML = IMatchLib.parseVariables(file, "{File.FileName}");
}

(not syntax checked, from the top of my head. See QR App or ReportApp for working examples).



PS. If you put your App into

apppanel\user\MyApp

the path to the IMatchLib must not be adjusted. The BasicApp uses

../../lib/imatch_app.js

which goes two up and then down into "lib". lib is on the same level as user, so MyApp->user->apppanel->lib is correct.


JohnZeman

  • Global Moderator
  • *****
  • Posts: 1400
  • I'm too damn old to act my age.
Thanks Mario, I've got the basic concept working now.  Spent all day on it, it now parses the variables and updates those variables each time a different image is selected.  But I've still got to figure out how to actually store what happens when a JavaScript function is called and then to use those same settings when a different image is selected but my brain is fried for the time being. :o

JohnZeman

  • Global Moderator
  • *****
  • Posts: 1400
  • I'm too damn old to act my age.
Whoa, what a pleasant surprise!  It turns out I didn't have to concern myself with saving what happens when a function is called, IMatch remembers it unless I reload either the app or the database. ;)

Looks like I might get this project done after all. ;D ;D ;D ;D ;D

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 28570
A HTML Template is reloaded every time a new image is selected (and also in response to some other events). This is required because IMatch needs to re-generate the HTML from the template by parsing the variables again.

The HTML code of an app is loaded once. When the active selection changes, IMatch notifies the App. But it does not reload the HTML code, so your context remains unchanged, variables keep their values etc. Until you close the App panel, you force a refresh or the database is closed.

Usually you only need to store settings in your App which should survive the current IMatch session. For example, the Report App stores the variables the user has configured. The Var Toy app stores the expression the user has entered, etc.

A good place to store this info is the onunload event sent by the browser when it closes. This happens when the user closes the App panel, or the panel is closed because the workspace is closed (switch to another workspace, IMatch closing etc). There are several ways to handle this event, but without falling back to JQuery, you usually link the event to a method in your script:

Code: [Select]
<script type="text/javascript">
    window.onunload = onUnload;
...

and then write the method similar to

Code: [Select]
function onUnload() {
    IMatchLib.getScriptSettings().Set("setting_name","value");
}

I suggest you look at the source of the VarToy app. It is very simple and you can easily see when the VarToy loads the saved expression string, and when it saves that string.



JohnZeman

  • Global Moderator
  • *****
  • Posts: 1400
  • I'm too damn old to act my age.
Got it!
When I toggle a metadata section from open to closed (or vice versa) that setting is saved then retrieved the next time the app is loaded.
I still have some general cleanup work to do yet, but basically everything seems to be working now. ;)

Thanks Mario!  ;D

[attachment deleted by admin]

Mario

  • IMatch Developer
  • Administrator
  • *****
  • Posts: 28570
Very nice  :)