Javascript field can't use function defined in the shared script ?

309 views
Skip to first unread message

stary -

unread,
Aug 10, 2021, 11:42:11 AM8/10/21
to mementodatabase
I have defined this sub function inside the shared script tab
however when i trying to call it from a javascript field it shows undefined errorScreenshot_2021-08-10-23-26-36-880_com.luckydroid.droidbase.jpg
what should  i do?
should  i create  a github repository or is there any other continent ways to add a user defined  javascript library ?

Beth Dixon

unread,
Aug 10, 2021, 11:50:31 AM8/10/21
to stary -, mementodatabase
Looks like you are missing a ) at the end of your script.

Beth

--
You received this message because you are subscribed to the Google Groups "mementodatabase" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mementodataba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mementodatabase/9b9c1d4a-cb29-445a-81f8-a7ed1821207fn%40googlegroups.com.

Beth Dixon

unread,
Aug 10, 2021, 11:51:12 AM8/10/21
to stary -, mementodatabase
Ps...and looks like you haven't added JavaScript libraries.

stary -

unread,
Aug 10, 2021, 11:57:32 AM8/10/21
to Beth Dixon, mementodatabase
yes, there's no add shared script options inside the add library 
Image
this is my shared script 

From: Beth Dixon <beth.ea...@gmail.com>
Sent: Tuesday, August 10, 2021 11:50:56 PM
To: stary - <sta...@live.com>
Cc: mementodatabase <memento...@googlegroups.com>
Subject: Re: Javascript field can't use function defined in the shared script ?
 

Beth Dixon

unread,
Aug 10, 2021, 1:29:06 PM8/10/21
to stary -, mementodatabase
Have you selected Add JavaScript Libraries??  I don't see that you have done that.  See attached screenshot.
Screenshot_20210810-132554_Gmail.jpg

Beth Dixon

unread,
Aug 10, 2021, 2:12:53 PM8/10/21
to stary -, mementodatabase
Just to clarify, go to edit of your library, scroll down to Add javascript libraries, tap on that, then select moment.min.js

Beth

stary -

unread,
Aug 10, 2021, 2:40:28 PM8/10/21
to Beth Dixon, mementodatabase
I have added the moment.min
but it still says sub(my defined function in shared script ) not found 
Image



From: Beth Dixon <beth.ea...@gmail.com>
Sent: Wednesday, August 11, 2021, 02:12
To: stary -
Cc: mementodatabase

Er Mo

unread,
Aug 10, 2021, 3:15:48 PM8/10/21
to mementodatabase
Hallo
Es gibt da 2 Möglichkeiten . Sie verwenden eine Trigger oder Auslöser . Da gebt es eine Möglickeit ein Skript für diese Bibliothek zu hinterlegen , die von den Sjripten aufgerufen werden kann . Oder sie möchten ein Skript für mehrer Bibliotheken zur verfügung haben . Da kann man in GitHub gehen oder auf einer Webseiten ablegen .

Hello
There are two options. You are using a trigger or trigger. There is a possibility to store a script for this library, which can be called by the scripts. Or you would like to have a script available for several libraries. You can go to GitHub or put it on a website.

Beth Dixon

unread,
Aug 10, 2021, 3:46:36 PM8/10/21
to stary -, mementodatabase
You are missing a bracket in your code.

Try this...

sub(field("link"),("bullet"))

Bill might be able to help you better than I.

But try the above...

Beth
processed.jpeg

Bill Crews

unread,
Aug 10, 2021, 4:35:09 PM8/10/21
to stary -, Beth Dixon, mementodatabase
We know only what we can glean ... That there is a field named "name" and a value "bullet" that you want to go into that field. If it's a shared script, why does it call a global function field()? There is no such global function. There must be a "current entry" for that, and shared scripts (like most other things) don't work for JavaScript fields.

If you tell us more, we can be of more help, but all I can do is guess that the following is what you want...

It must be a trigger script or an action script located in an entry. This enables the entry() global function to work, so there is a "current" entry.

So, entry().set("name", "bullet); is all the script you need, if your objective is to set that field's value to "button". If you're wanting to do something else, you might consider telling what that is.



Screenshot_2021-08-10-23-26-36-880_com.luckydroid.droidbase.jpg
processed.jpeg

Bill Crews

unread,
Aug 10, 2021, 5:17:38 PM8/10/21
to stary -, Beth Dixon, mementodatabase
If you want to extend Memento with JavaScript, I suggest you read the wiki, so you can learn how to do it. If you want to use the simplest scripts, you could try using a JavaScript field, but it doesn't have fancy global functions like set() or link() or libByName(), but it does have simple global functions like field();

If you decide a usually simple JavaScript field is for you, read this page...
JavaScript field, or...

If a fancy, probably longer script is what you feel you need, read this page, for starters, since all the other stuff is based on: Memento Javascript Library, or...

If you're going for the fancier stuff & have read the MJL page, you can then branch out into various directions, depending on what you want to do next...

 * For a button in the libraries list screen or a button in an edit view card (assuming mobile Memento): Actions, or...

 * For a script to run automatically when certain things happen in certain circumstances: Triggers, or...

...and...
Trigger examples: Trigger examples, or...

The rest of the MJL-based JavaScript pages are for other, less-often-used scripts...
https://wiki.mementodatabase.com/index.php/How:Write_scripts_in_JavaScript_for_Memento, or...

And once you do that, if you still have questions or comments, please post here in the forum.

Bill Crews

unread,
Aug 10, 2021, 5:25:16 PM8/10/21
to stary -, Beth Dixon, mementodatabase
CORRECTION: Actually, for a shared function definition, it should be...

function sub(fieldname, fieldvalue) {
    entry().set(fieldName, fieldValue);
}

Then call that in a trigger, action, or whatever with this...

sub("name", "bullet");

On Tue, Aug 10, 2021, 16:34 Bill Crews <bill....@gmail.com> wrote:

stary -

unread,
Aug 11, 2021, 4:57:54 AM8/11/21
to mementodatabase

Thanks so much for replying.
Actually what i want to do is extend the Entry Class to add a method that can find any entry it links to matches a specific name.
For example :
A entry named A1 link to B1 and B2
then B1 link to C1 and C2
so this is like a binary tree structure with 3 layers from root A1 to leaf C1 or C2.
Then, since every node is Entry type, I want to extend the Entry Class to add a extra method "sub" to search if any child node matches the required name.
Untitled1_20210811164231.png
eg:
A1.sub("B1") should return B1 note as an entry type because B1's field "name" is "B1"
or in another word:
A1.field("link").find(x=>x.field("name")=="B1")

similarly:
A1.sub("B1").sub("C2") should return C2 as an entry type 

I have successfully wrote the sub as a global function by following definitions:
(see picture below)Screenshot_2021-08-11-16-49-15-508_com.android.browser.jpg

it can be called like sub(A1,"B1") and works fine.
but what I actually wanna do is to add it into the Entry class as a method so i can write a clear and beautiful chain as A1.sub("B1").sub("C2").sub("D3)...... instead of ugly and hard to read nested function like sub(sub(sub(A1,"B1"),"C2"),D3)......

Bill Crews

unread,
Aug 11, 2021, 12:27:05 PM8/11/21
to stary -, mementodatabase
Ah! Excellent! By providing this information, someone may, at least to a limited extent, be able to help you here. Trees within relational databases have always been hard to represent, and it's been requested & discussed many times in the forum for Memento.

I didn't have any inkling that you're as conversant in JS as you are. Regardless of the outcome, we can have a much more efficient conversation now.

By the way, like some other JS features, the this keyword, though it works in the desktop edition of Memento, may not work in the mobile edition that has an older version of JS built into it. The other troublesome keywords I'm aware of are let, const, and for...of, so if you want your scripts to run on mobile devices, you'll need to use entry(), var, and for...in instead.

I'm about to spend some time working to help you, but I'll have to take a mid-day break now. In the meantime, I suggest reading at the following link. I haven't tried prototyping (like subclassing) in JS, but that's where it is described, and if it's possible in Memento, I think that page and it's nearby friends may be where how to do it is described...


I am extremely interested in this subject! I hope you don't drop it and might share with us what you may learn. I'll be back after my mid-day break...

Er Mo

unread,
Aug 11, 2021, 3:30:44 PM8/11/21
to mementodatabase
Hallo
Wenn sie in einer Bibliothek in Memento ein ein Skript mehrmals verwenden wollen schreiben sie es unter "Shared " . Zum Aufrufen von den Skript muss der Name unter den das Skript gespeichert ist,  angegeben werden . Ist nicht wie bei einer Funktion .

Hello
If you want to use a script several times in a library in Memento, write it under "Shared". To call the script, the name under which the script is saved must be specified. It's not like a function.

Bill Crews

unread,
Aug 11, 2021, 6:30:52 PM8/11/21
to stary -, mementodatabase
If nothing else works, I think Treeline or maybe Treepad are probably the best of the fielded tree apps.

Bill Crews

unread,
Aug 11, 2021, 6:32:58 PM8/11/21
to stary -, mementodatabase
Even if this would work, it would need a pair of parentheses, so...
var e = entry();
e.prototype.sub = function() (
    <Your code here>
}

Call it with e.sub()
On Wed, Aug 11, 2021, 6:05 PM Bill Crews <bill....@gmail.com> wrote:
If I understand correctly, you should be able to use something like the following...

In a Shared script:
var e = entry();
e.prototype.sub = function() (
    <Your code here>
}

I'm not sure, but I think the => operator isn't there in the Memento version of JavaScript.

stary -

unread,
Aug 11, 2021, 11:28:30 PM8/11/21
to Bill Crews, mementodatabase
Yeah, I do ttied these before.

Image
I think e.prototype is undefined for some reason .

From: Bill Crews <bill....@gmail.com>
Sent: Thursday, August 12, 2021 6:32:40 AM

stary -

unread,
Aug 11, 2021, 11:49:47 PM8/11/21
to Bill Crews, mementodatabase
As you can see , I can successfully acquired linked object by my sub function Image
however the prototype of this object has the same error: undefinedImage

here is my library :
you may test with it.

From: memento...@googlegroups.com <memento...@googlegroups.com> on behalf of stary - <sta...@live.com>
Sent: Thursday, August 12, 2021, 11:28
To: Bill Crews
Cc: mementodatabase
You received this message because you are subscribed to a topic in the Google Groups "mementodatabase" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mementodatabase/FCIlf0__q3g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mementodataba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mementodatabase/OSBPR01MB259740F2751CD1774C833EFEE3F99%40OSBPR01MB2597.jpnprd01.prod.outlook.com.

Reply all
Reply to author
Forward
0 new messages