Returning a JS object to Anvil

I’m using an external JS library (https://codemirror.net) in a custom HTML setting. It all works fine (link at the end of the post).

However, if I want more than one instance in my app, I have to maintain a global array of codemirror instances and reference them using a name.

What would be neater is if I could return the newly created instance to Anvil, and Anvil could then send it as a parameter to its call_js functions when manipulating the codemirror instance.

Problem is, Anvil won’t accept a JS object being assigned to an Anvil one. Which I get.

Is there a way around this?

I tried to serialise it (I even spelled it with a ‘z’) but the serialisation process complained of cyclic references. Stack Overflow had several suggestions for creating my own serialisation function that would get around that. It’s an option, but I’d appreciate the opinions of people far better at JS than me first :slight_smile:

Here’s the promised link -

1 Like

Edit: This feature has now shipped! Check out the Using Javascript section of the Anvil documentation :slight_smile:

Original reply, from 2018:

I’ve moved this to Feature Requests. We would love to (eg) automatically wrap any JS value in an opaque “JSReference” class, and then turn it back into that JS object when we next do a call_js(). We’ve added it to our (long) TODO list; make sure to click “Like” if it’s something you want!

3 Likes