bugfix: make ajax module work
This commit is contained in:
parent
30491d401a
commit
cf9b22ae62
2 changed files with 24 additions and 17 deletions
40
src/ajax.nim
40
src/ajax.nim
|
|
@ -12,24 +12,30 @@ proc ajax(meth, url: cstring; headers: openarray[(cstring, cstring)];
|
|||
cont(httpStatus, response)
|
||||
redraw()
|
||||
|
||||
proc setRequestHeader(a, b: cstring) {.importc: "ajax.setRequestHeader".}
|
||||
{.emit: """
|
||||
var ajax = new XMLHttpRequest();
|
||||
ajax.open(`meth`,`url`,true);""".}
|
||||
type
|
||||
HttpRequest {.importc.} = ref object
|
||||
ThisObj {.importc.} = ref object
|
||||
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):
|
||||
setRequestHeader(a, b)
|
||||
{.emit: """
|
||||
ajax.onreadystatechange = function(){
|
||||
if(this.readyState == 4){
|
||||
if(this.status == 200){
|
||||
`contWrapper`(this.status, this.responseText);
|
||||
} else {
|
||||
`contWrapper`(this.status, this.statusText);
|
||||
}
|
||||
}
|
||||
}
|
||||
ajax.send(`data`);
|
||||
""".}
|
||||
ajax.setRequestHeader(a, b)
|
||||
ajax.statechange proc() =
|
||||
if this.readyState == 4:
|
||||
if this.status == 200:
|
||||
contWrapper(this.status, this.responseText)
|
||||
else:
|
||||
contWrapper(this.status, this.statusText)
|
||||
ajax.send(data)
|
||||
|
||||
proc ajaxPut*(url: cstring; headers: openarray[(cstring, cstring)];
|
||||
data: cstring;
|
||||
|
|
|
|||
|
|
@ -223,6 +223,7 @@ when false:
|
|||
var drawTimeout: Timeout
|
||||
|
||||
proc dodraw() =
|
||||
if dorender.isNil: return
|
||||
let newtree = dorender()
|
||||
newtree.id = "ROOT"
|
||||
toFocus = nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue