bugfix: make ajax module work

This commit is contained in:
Andreas Rumpf 2017-04-24 17:45:40 +02:00
commit cf9b22ae62
2 changed files with 24 additions and 17 deletions

View file

@ -12,24 +12,30 @@ proc ajax(meth, url: cstring; headers: openarray[(cstring, cstring)];
cont(httpStatus, response) cont(httpStatus, response)
redraw() redraw()
proc setRequestHeader(a, b: cstring) {.importc: "ajax.setRequestHeader".} type
{.emit: """ HttpRequest {.importc.} = ref object
var ajax = new XMLHttpRequest(); ThisObj {.importc.} = ref object
ajax.open(`meth`,`url`,true);""".} readyState, status: int
responseText, statusText: cstring
proc setRequestHeader(r: HttpRequest; a, b: cstring) {.importcpp: "#.setRequestHeader(@)".}
proc statechange(r: HttpRequest; cb: proc()) {.importcpp: "#.onreadystatechange = #".}
proc send(r: HttpRequest; data: cstring) {.importcpp: "#.send(#)".}
proc open(r: HttpRequest; meth, url: cstring; async: bool) {.importcpp: "#.open(@)".}
proc newRequest(): HttpRequest {.importcpp: "new XMLHttpRequest(@)",}
var this {.importc: "this".}: ThisObj
let ajax = newRequest()
ajax.open(meth, url, true)
for a, b in items(headers): for a, b in items(headers):
setRequestHeader(a, b) ajax.setRequestHeader(a, b)
{.emit: """ ajax.statechange proc() =
ajax.onreadystatechange = function(){ if this.readyState == 4:
if(this.readyState == 4){ if this.status == 200:
if(this.status == 200){ contWrapper(this.status, this.responseText)
`contWrapper`(this.status, this.responseText); else:
} else { contWrapper(this.status, this.statusText)
`contWrapper`(this.status, this.statusText); ajax.send(data)
}
}
}
ajax.send(`data`);
""".}
proc ajaxPut*(url: cstring; headers: openarray[(cstring, cstring)]; proc ajaxPut*(url: cstring; headers: openarray[(cstring, cstring)];
data: cstring; data: cstring;

View file

@ -223,6 +223,7 @@ when false:
var drawTimeout: Timeout var drawTimeout: Timeout
proc dodraw() = proc dodraw() =
if dorender.isNil: return
let newtree = dorender() let newtree = dorender()
newtree.id = "ROOT" newtree.id = "ROOT"
toFocus = nil toFocus = nil