moved ajax support into its own module and make the callback call 'redraw' automatically
This commit is contained in:
parent
037ddef91d
commit
27cacbd271
2 changed files with 42 additions and 32 deletions
41
src/ajax.nim
Normal file
41
src/ajax.nim
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
## Karax -- Single page applications for Nim.
|
||||
|
||||
## This module implements support for `ajax`:idx: socket
|
||||
## handling.
|
||||
|
||||
import karax
|
||||
|
||||
proc ajax(meth, url: cstring; headers: openarray[(cstring, cstring)];
|
||||
data: cstring;
|
||||
cont: proc (httpStatus: int; response: cstring)) =
|
||||
proc contWrapper(httpStatus: int; response: cstring) =
|
||||
cont(httpStatus, response)
|
||||
redraw()
|
||||
|
||||
proc setRequestHeader(a, b: cstring) {.importc: "ajax.setRequestHeader".}
|
||||
{.emit: """
|
||||
var ajax = new XMLHttpRequest();
|
||||
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`);
|
||||
""".}
|
||||
|
||||
proc ajaxPut*(url: string; headers: openarray[(cstring, cstring)];
|
||||
data: cstring;
|
||||
cont: proc (httpStatus: int, response: cstring)) =
|
||||
ajax("PUT", url, headers, data, cont)
|
||||
|
||||
proc ajaxGet*(url: string; headers: openarray[(cstring, cstring)];
|
||||
cont: proc (httpStatus: int, response: cstring)) =
|
||||
ajax("GET", url, headers, nil, cont)
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import dom, vdom, jstrutils, components, jdict
|
||||
|
||||
export dom.Event #, dom.cloneNode, dom
|
||||
export dom.Event
|
||||
|
||||
proc kout*[T](x: T) {.importc: "console.log", varargs.}
|
||||
## the preferred way of debugging karax applications.
|
||||
|
|
@ -281,37 +281,6 @@ proc setOnHashChange*(action: proc (hashPart: cstring)) =
|
|||
redraw()
|
||||
onhashchange = wrapper
|
||||
|
||||
proc ajax(meth, url: cstring; headers: openarray[(cstring, cstring)];
|
||||
data: cstring;
|
||||
cont: proc (httpStatus: int; response: cstring)) =
|
||||
proc setRequestHeader(a, b: cstring) {.importc: "ajax.setRequestHeader".}
|
||||
{.emit: """
|
||||
var ajax = new XMLHttpRequest();
|
||||
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){
|
||||
`cont`(this.status, this.responseText);
|
||||
} else {
|
||||
`cont`(this.status, this.statusText);
|
||||
}
|
||||
}
|
||||
}
|
||||
ajax.send(`data`);
|
||||
""".}
|
||||
|
||||
proc ajaxPut*(url: string; headers: openarray[(cstring, cstring)];
|
||||
data: cstring;
|
||||
cont: proc (httpStatus: int, response: cstring)) =
|
||||
ajax("PUT", url, headers, data, cont)
|
||||
|
||||
proc ajaxGet*(url: string; headers: openarray[(cstring, cstring)];
|
||||
cont: proc (httpStatus: int, response: cstring)) =
|
||||
ajax("GET", url, headers, nil, cont)
|
||||
|
||||
{.push stackTrace:off.}
|
||||
|
||||
proc setupErrorHandler*(useAlert=false) =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue