Comments (5)
Hi,
You should try a different approach, which may require some experimenting:
- Do not add a reference to the js file to the index.html file. Instead add a NPM dependency in your
build.gradle
in thekotlinFrontend
section like this:
kotlinFrontend {
npm {
dependency("mongodb-stitch-browser-sdk", "4.3.1")
}
// ...
}
If we are talking about Electron app, you should probably consider using "mongodb-stitch-server-sdk" instead.
- In the KVision application use it like this (haven't tested it):
val stitch = require("mongodb-stitch-browser-sdk")
val app = stitch.defaultAppClient
- Both
stitch
andapp
object will be ofdynamic
type, which lets you run any code on them. You can also declare some external classes. Try something like this:
external class UserPasswordCredential(user: String, password: String)
val credential = UserPasswordCredential("<email>", "<password>")
val promise: kotlin.js.Promise = app.auth.loginWithCredential(credential)
- You can try finding typescript definitions for this sdk and try to convert them to Kotlin with
ts2kt
tool. It should give you all external classes and interfaces definitions ready to use. But I'm having problems finding it, because this sdk is already written in typescript, so it doesn't have definitions - just source files ;-) I don't know how to convert such thing at the moment. Anyway, you should be able to work with dynamic types only.
from kvision.
And to answer the more general question you asked. To run javascipt code from kotlin you can:
- use
js("..")
function e.g.
val app = js("Stitch.defaultAppClient")
val credential = js("new UserPasswordCredential('<email>', '<password>')")
- Use
dynamic
type like I've shown in the previous comment. - Write all your JS code inside independent functions in some external js file, reference that file in the main index.html and use kotlin js() function to call it.
You can find more info here and here
from kvision.
Thanks for that! I got all the way up to defining the UserPasswordCredential('<email>', '<password>')
The Stitch and UserPasswordCredential classes are defined within the mongodb-stitch-browser-sdk module.
I tried to define an external class with nested classes like this:
@JsModule("mongodb-stitch-browser-sdk")
@JsNonModule
external class stitch {
class Stitch {
val defaultAppClient: Any?
fun initializeDefaultAppClient(clientAppId: String)
}
class UserPasswordCredential(user: String, password: String)
}
However I get an unresolved reference issue with stitch.Stitch.initializeDefaultAppClient
and stitch.Stitch.defaultAppClient
, any idea why? I can feel I'm close!
from kvision.
I've tested the following code and it seems to be working (but I don't have a mongodb account so can't be sure). Note, I have used mongodb-stitch-server-sdk
as a npm dependency in build.gradle, because browser-sdk gives some errors with electron api.
import pl.treksoft.kvision.utils.createInstance
val stitchModule = require("mongodb-stitch-server-sdk")
val stitch = stitchModule.Stitch
val userPasswordCredentialClass: Any = stitchModule.UserPasswordCredential
val credential: Any = userPasswordCredentialClass.createInstance("user", "pass")
val client = stitch.initializeDefaultAppClient("app-id")
val promise: Promise<dynamic> = client.auth.loginWithCredential(credential)
promise.then { user: dynamic ->
console.log(user)
client.close()
}.catch { err ->
console.log(err)
client.close()
}
from kvision.
Yes that worked, createInstance seems to be the thing I was missing. Thanks for your help 👍
from kvision.
Related Issues (20)
- Request - support for crossorigin attribute in Image (and others) HOT 1
- Ability to intercept image load errors HOT 2
- Component lifecycle HOT 6
- StackPanel seems to not indexing components properly HOT 4
- Wrong module in kvision.gitbook.io. HOT 1
- trying to generate .pot files HOT 12
- overrides of `SimplePanel.AddAll` call refresh outside of a `singleRender` wrapper. HOT 1
- Allow KVService to have arguments with GET HOT 6
- Allow modifying response headers for Spring Boot and Micronaut apps HOT 1
- remove id parameter on get requests HOT 1
- Can't get data from Tabulator.RowComponent HOT 2
- Expandable/collapsible fieldset or panel HOT 2
- How to attch files using the Richtext Component HOT 7
- duplicated different word in kvision docs HOT 1
- did you consider to integrate kvision with ktor client? HOT 5
- can't make tabulator selectable with kvision v7.4.4 HOT 2
- My tabulator not selecting data HOT 5
- How to add more than one bottomCalc rows? HOT 2
- kvision gradle plugin not works well with Spring-Web-Flux HOT 4
- DropDown - call method instead of loading a route HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kvision.