Created
March 12, 2019 23:22
-
-
Save postite/1c638b00ff85ae0a3c03f503b73e8904 to your computer and use it in GitHub Desktop.
tink_web & coconut minimal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package minimal; | |
#if client | |
import tink.web.proxy.Remote; | |
import tink.url.Host; | |
import tink.http.clients.JsClient; | |
import tink.web.proxy.Remote.RemoteEndpoint; | |
import js.Browser.document as doc; | |
using tink.CoreApi; | |
import coconut.Ui.hxx; | |
#else | |
using tink.CoreApi; | |
import tink.web.proxy.Remote; | |
import tink.http.containers.*; | |
import tink.web.routing.*; | |
import tink.http.Response; | |
import tink.http.middleware.Static; | |
#end | |
import tink.json.Representation; | |
interface IRemoteRoot{ | |
@:get("/getrec") | |
public function getrec():Promise<RecX>; | |
} | |
/* server side */ | |
class MiniApp{ | |
static function main() { | |
#if client | |
Client.main(); | |
#else | |
var container = new NodeContainer(8080); | |
var router = new Router<Root>(new Root()); | |
var handler:tink.http.Handler = function(req) { | |
return router.route(Context.ofRequest(req)).recover(OutgoingResponse.reportError); | |
} | |
handler = handler.applyMiddleware(new Static('./', '/')); | |
container.run(handler); | |
#end | |
} | |
} | |
#if server | |
class Root implements minimal.IRemoteRoot{ | |
public function new() {} | |
@:get("/") | |
@:html(function(data) return'<!DOCTYPE html> | |
<head><script src="minimalclient.js"></script></head> | |
<body>${data.bim}</body> | |
</html>') | |
public function home(){ | |
return return { | |
bim: "hello" | |
}; | |
} | |
@:produces('application/json') | |
@:get("/getrec") | |
public function getrec():Promise<RecX>{ | |
var r= new Rec(); | |
r.id=1; | |
r.title="one"; | |
r.desc="the one"; | |
return Promise.lift(r); | |
} | |
} | |
#end | |
/* client side */ | |
#if client | |
class Client{ | |
static var remote:Remote<IRemoteRoot>; | |
public static function main(){ | |
remote = new Remote<IRemoteRoot>( | |
new JsClient(), | |
new RemoteEndpoint(new Host('localhost:8080')) | |
); | |
doc.addEventListener("DOMContentLoaded",e->{ | |
var reco=new CocoRec({ title : "nope", id : 0, desc : "nope" }); | |
remote.getrec().next(rec->{ | |
//reco.setTitle(rec.title); | |
reco.setAll(rec); | |
return rec; | |
} | |
).recover(r->{r.log();return null;}); | |
coconut.ui.Renderer.mount( | |
cast doc.body.appendChild( doc.createDivElement()) | |
,hxx('<CocoApp model=${reco} />') | |
); | |
}); | |
} | |
} | |
class CocoApp extends coconut.ui.View{ | |
static public var remote:tink.web.proxy.Remote<IRemoteRoot>; | |
@:attribute var model:CocoRec; | |
function render() | |
<div class="cocoapp" > | |
<h1>{model.title}</h1> | |
</div>; | |
} | |
class CocoRec implements coconut.data.Model{ | |
@:observable public var id:Int; | |
@:observable public var title:String; | |
@:observable public var desc:String; | |
@:transition | |
public function setTitle(t:String){ | |
trace( "setTitle"); | |
return {title:t}; | |
} | |
@:transition | |
public function setAll(recx:RecX){ | |
return @patch recx; | |
} | |
} | |
#end | |
@:structInit | |
class Rec{ | |
@:optional public var id:Int; | |
@:optional public var title:String; | |
@:optional public var desc:String; | |
public function new(){ | |
} | |
public static function create(args:Dynamic):minimal.Rec{ | |
var t:Rec= args; | |
return t; | |
} | |
} | |
@:forward | |
abstract RecX(minimal.Rec) from minimal.Rec to minimal.Rec{ | |
public inline function new(v) this = v; | |
@:to function toRepresentation():Representation<String> | |
return new Representation(tink.Json.stringify(this)); | |
@:from static function ofRepresentation(rep:Representation<String>){ | |
return new RecX(minimal.Rec.create(tink.Json.parse(rep.get()))); | |
} | |
} | |
/*minimalServer.hxml*/ | |
/* | |
-cp src | |
-lib hxnodejs | |
-lib tink_web | |
-lib tink_template | |
-lib tink_http_middleware | |
-D server | |
-main minimal.App | |
-js minimalserver.js | |
*/ | |
/*minimalClient.hxml*/ | |
/* | |
-cp src | |
-main minimal.App | |
-lib coconut.data | |
-lib coconut.ui | |
-lib coconut.vdom | |
-lib tink_web | |
-lib tink_core | |
-D client | |
-js minimalclient.js | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment