4

Put comments in the scripts

I discovered that it is possible to easily metre comments in scripts with a notation that was not documented:

#{here I can put any text}#;
var y := year('Start date');
var m := month('Start date');
#{here again}#;
var d := day('Start date');
var t := for i from 0 to days('Start date', 'End date' + 1) do
        {
            year: year(date(y, m, d + i)),
            month: month(date(y, m, d + i)),
            day: day(date(y, m, d + i));
            #{
*********** and I can use it to remove temporary one part of
                                my code to try another think *********

                year: year(date(y, m, d + i)),
            month: month(date(y, m, d + i)),
            day: day(date(y, m, d + i))
********************************************************************************}#;
            #{ --- usually, you could use quotation marks to transform a part
                of the code into a comment. The defect is that it was not
                possible to put in comment a string of character string.
                With this new notation, any type of code can be commented ---

            var s := " one string in comment ";
            -------------------------------------------------------}#
        }
    end;
for y in unique(t.year) do
    for m in unique(t[year = y].month) do
        format(date(number(y), number(m), 1), "YYYY MM : ") + count(t[year = y and month = m]) + " days
"
    end
end;
#{
    The only flaw of this is that now we have
    no excuse not to comment our code 😅
}#

put #{ to begin comment block and finish by }#

...

To be honest, I have to tell you that this is not a comment notation. It was intended to be used to put JavaScript code in Ninox Script, but it was never implemented. I found out that this notation has been there for at least a year (maybe since the beginning?)

If you don't want to use it to put comments, and you want to use it to write JavaScript, you have to initialize two functions with a simple code on the formula. This formula must be executed once after opening the database :

html("
<script>
queries.JS.prototype._compileSyncJS = function(e) {return `( () => {${this.code} } )()`}
queries.JS.prototype._compileAsyncJS = function(e) {return `(function(cb){cb(( () => {${this.code}})())})`}
</script>
")

Then you can use the JavaScript code anywhere in your Ninox script. For example, put this code behind a button and press it :

var r := #{return window.prompt("Let my know your name", "nobody")}#;
alert("hello " + r)

In line 1, the variable r is initialized with the result of the JavaScript prompt function which asks the user for a value.
On line 2, the result of the input is displayed using the alert function of Ninox.

17replies
{{batchActionsCtrl.state.selectedCount}} {{batchActionsCtrl.state.selectedCount==1?'reply':'replies'}} selected Move Cancel
Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
    • Fred
    • Fred
    • 2 mths ago
    • Reported - view
    Jacques TUR said:
    #{
    The only flaw of this is that now we have
    no excuse not to comment our code
    }#

     Jacques does it again! Now I'm going to have to comment my code. Though I wonder if Ninox will implement the JavaScript feature in the future and then what will happen to our new comment code? Will Ninox try to run our comments and of course error out? Will Ninox report that all of our code with comments are now full of errors?

    Reply Like
    • Fred Yes, that is a very good question. I'm trying to update the Ninox code to accept both indifferently, JavaScript and/or comments. I will come soon with a solution for all (I hope 🤞).

      Reply Like
    • Sean
    • Sean
    • 2 mths ago
    • Reported - view

    Jacques TUR

    When I try the window.prompt code snippet, the prompt window doesn't display and r is null. I'm using the Mac version.

    Reply Like
    • Sean Sorry, the prompt function does not exist in the Mac app. But M. Daaboul  told me that it was possible to insert HTML in the text of the Dialog function. I think that with this it is possible to remake the prompt function by adding an input field and retrieve the value.
       

      Reply Like
    • Sean I found a solution about prompt for Mac app and Web. I use the solution of M. Daaboul  with the dialog function and NativeJS :

      var ret := dialog("Title of dialog box", ---
      <div class='component editor  editor-string' style="display : block;">
      <label for='fname'>Whats your name is ?</label>
      <input type='text' class='nx-input nx-input__value' id='fname' name='fname' value='your name' onchange='window._dialogValue = this.value;'></div>
      ---, ["cancel", "ok"]);
      if ret = "ok" then
          var r := #{:text return window._dialogValue}#;
          alert("result is : " + r)
      end

      The onchange event of the input element (end of line 4) puts the entered value in the global JavaScript variable _dialogValue.
      Then, if the result of the dialog is "ok", put the value of _dialogValue in the variable r Ninox (line 7).

      Note: I put several element nesting to get the same visual result as a Ninox field. But you can just put an input element (line 4) and it works just as well. 

      A more structured, reusable solution might look like this: 

      function promptText(titleBox : text,caption : text,defaultValue : text) do
          var ret := dialog(titleBox, ---
      <div class='component editor  editor-string' style="display : block;"><label for='fname'>{ caption }</label><input type='text' class='nx-input nx-input__value' id='fname' name='fname' value='{ defaultValue }' onchange='window._dialogValue = this.value;'></div>
      ---, ["cancel", "ok"]);
          {
              validate: ret = "ok",
              value: #{:text return window._dialogValue}#
          }
      end;
      var r := promptText("Title of dialog box", "Whats your name is ?", "your name");
      if r.validate then
          alert("result : " + r.value)
      end
      
      Reply Like
      • Sean
      • Sean
      • 2 mths ago
      • Reported - view

      Jacques TUR Thank you. I appreciate your work as I know others do. I ran the code and everything works except displaying the input value. The alert window just shows "result : ". This is not something I need for anything I am working on now. Just thought you would like the feedback.

      Reply Like
    • Sean Thank's for your help. 

      Maybe it is because NativeJS is not initialized on Ninext ? See here to do so.

      I have attached an example database. Let me know if it work on your mac (or web) app.

      • Sean
      • Sean
      • 2 mths ago
      • Reported - view

      Jacques TUR 🤦‍♂️ I admit that I hadn't been keeping up with the rest of the class and forgot to initialize nativeJS. It did work except for not displaying the info entered. Here is a screenshot from my console...

      but after I copy and pasted the code from here into "Trigger after open" I get this error...

      Reply Like
    • Sean Sean That's strange, I copied the same code to my console and everything works fine (app and wab). Have you tried the Test prompt.ninox that I attached just before?

      Reply Like
      • Sean
      • Sean
      • 2 mths ago
      • Reported - view

      Jacques TUR I hadn't, but I just tried that too...

      Reply Like
    • Sean Surprising 🤔. wich version of Ninox do you have ?

      Reply Like
      • Sean
      • Sean
      • 2 mths ago
      • Reported - view

      Jacques TUR According to macOS Finder 3.6.1 😆. I find that a little difficult to believe since the current version is 3.7.6 and the last creation date on my computer was July 20, 2022.

      Reply Like
    • Sean The best is to look directly in the application:

      Reply Like
      • Sean
      • Sean
      • 2 mths ago
      • Reported - view

      Jacques TUR It showed the same as Finder. I just updated it and your code works fine. Any idea when Ninox moved to 3.7...?

      Reply Like
    • Sean That's good! 
      I tried to compare the 3.5 version (I don't have the 3.6) and the current 3.7 and I don't see anything that can explain this problem!?
      If anyone has an older version, I want them to try it and tell me if it works or not.

      Reply Like
    • Jacques TUR It is also possible not to use NativeJS for the prompt. It is necessary to use a temporary field (for example a field in global memory):

      var t := this;
      var r := {
              titleBox: "Title of dialog box",
              caption: "Whats your name is ?",
              defaultValue: "your name",
              returnField: "_Value"
          };
      var body := ---
      <div class='component editor  editor-string' style="display : block;"><label for='fname'>{ r.caption }</label><input type='text' class='nx-input nx-input__value' id='fname' name='fname' value='{ r.defaultValue }'
      onchange='
      var field = database.schema.typeOf("{ string(t.Id) }").findElement("{ r.returnField }");
      database.update("{ string(t.Id) }", field.id, this.value, null );
      '></div>
      ---;
      var ret := dialog(text(r.titleBox), body, ["cancel", "ok"]);
      if ret = "ok" then
          'Résult of prompt' := _Value
      end
      

      We then use the JavaScript of the function onchange to call the code of Ninox and thus update the field _Value (line 11 and 12).

      It is less flexible, but there is no need for an add-on.

      See the example in attachment.

  • I continued to explore the use of #{}# to insert JavaScrip in Ninox. The results are here.

    Reply Like
  • Spam
    • Remove post
    • Remove and block user
  • Edit
  • Publish
  • Unschedule
  • Cancel event
  • Uncancel event
  • Set status
    • Clear status
  • Pin
  • Unpin
  • Bump
  • Open replies
  • Close replies
  • Clear flags
  • Move
    • Move to category:
    • Learn & Share
    • Get help
    • Webinars
  • Copy
    • Copy to category:
    • Current category
    • Learn & Share
    • Get help
    • Webinars
  • Change type
    • Change topic type to:
    • Discussion
    • Question
    • Idea
    • Issue
    • Article
    • Event
  • Restore post
    • Spam
    • Offensive
    • Inappropriate
    • Duplicate
    • Other
  • Remove post
    • Spam
    • Offensive
    • Inappropriate
    • Duplicate
    • Other
  • Publish
  • Spam
    • Remove post
    • Remove and block user
  • Edit
  • Pin
  • Unpin
  • Open replies
  • Close replies
  • Label
    • Clear label
  • Move
  • Clear flags
  • Restore post
    • Spam
    • Offensive
    • Inappropriate
    • Duplicate
    • Other
  • Remove
    • Spam
    • Offensive
    • Inappropriate
    • Duplicate
    • Other
Like4 Follow
  • 4 Likes
  • 2 mths agoLast active
  • 17Replies
  • 224Views
  • 6 Following