Comments (8)
pl.treksoft.kvision.utils.obj
is a helper function to create a dynamic JS objects. obj { q = "kvision" }
is an equivalent of js("{\"q\": \"kvision\"}")
but looks a lot better :)
from kvision.
You can use kotlinx.serialization library (already a dependency of KVision) for (de)serialization and pl.treksoft.kvision.remote.CallAgent
class to make calls to your backend. A working example:
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.asDeferred
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.list
import pl.treksoft.kvision.hmr.ApplicationBase
import pl.treksoft.kvision.remote.CallAgent
import pl.treksoft.kvision.utils.obj
@Serializable
data class Repository(val id: Int, val full_name: String?, val description: String?, val fork: Boolean)
object App : ApplicationBase {
val callAgent = CallAgent()
override fun start(state: Map<String, Any>) {
GlobalScope.launch {
val result: dynamic = callAgent.remoteCall("https://api.github.com/search/repositories", obj { q = "kvision" }).asDeferred().await()
val items: List<Repository> = Json.nonstrict.parse(Repository.serializer().list, JSON.stringify(result.items))
println(items)
}
}
}
The remoteCall
method returns kotlin.js.Promise
object, so you can use it without coroutines if you want. I will think about adding some helper methods to CallAgent class to simplify above code and I'll put similar example in the docs.
from kvision.
Thanks for the great example! Would DynamicObjectParser
be useful or essentially equivalent for the parsing?
from kvision.
You are right! This is simpler and probably more efficient:
val items: List<Repository> = DynamicObjectParser().parse(result.items, Repository.serializer().list)
from kvision.
From KVision 0.0.25, it will be enough to write the whole call as:
val items: List<Repository> = callAgent.remoteCall("https://api.github.com/search/repositories", obj { q = "kvision" }, Repository.serializer().list) {
it.items
}.asDeferred().await()
from kvision.
In your 0.0.25 example, what is obj { q = "kvision" },
?
from kvision.
Fully type-safe calls with 0.0.25 will be possible as well:
@Serializable
data class Query(val q: String?)
@Serializable
data class SearchResult(val total_count: Int, val incomplete_results: Boolean)
GlobalScope.launch {
val searchResult: SearchResult = callAgent.call<SearchResult, Query>("https://api.github.com/search/repositories", Query("kvision")).asDeferred().await()
println(searchResult)
}
from kvision.
Version 0.0.25 is released. The KVision guide is updated as well.
from kvision.
Related Issues (20)
- 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
- Page loader how to. HOT 4
- Gantt and Calendar view of tasks HOT 7
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.