Hi,
I have a very hard time getting the simplest jsonCall working. I want
to use the select2 JS library as shown on
<
http://ivaynberg.github.com/select2/#data>,
i.e. I need to provide a JS function that takes a "query" parameter
(where query.term is the current search term), computes the search
results and calls query.callback with a "result" object.
My first approach was to use the following simple template code:
<script type="text/javascript"
class="lift:WebUtils.ajaxImageSelectFun" />
<script type="text/javascript">
$("#imgselect-widget").select2({ query: selectImg });
</script>
and have my snippet do something like:
def ajaxImageSelectFun = {
"script *" #> {
val handleJson = SHtml.jsonCall(JE.JsRaw("query"),
(json: JValue) => { ... })
JsCmds.Function("selectImg", List("query"), handleJson.cmd)
}
}
This looks pretty easy, but the problem is that I can't just do
JE.Call("query.callback", myResult)
within the jsonCall, as "query" is not within the context any more when
the function is executed. So I discovered the Context parameter for
jsonCall and used it as follows:
def ajaxImageSelectFun = {
"script *" #> {
val handleJson = SHtml.jsonCall(JE.JsRaw("query"),
new JsContext(Full("success"), Empty),
(json: JValue) => { ... })
JsCmds.Function("selectImg", List("query"),
JE.JsRaw(
"""
function success(data, textStatus, jqXHR) {
query.callback(data);
};
""" + handleJson.toJsCmd).cmd)
}
}
This already looks more complicated, but the major problem is that I
don't know how to return plain JSON from my (JValue => JsCmd) function
in jsonCall. In fact, the purpose of jsonCall seems to be to execute a
server-defined JavaScript function on the client, but I just want to
return some JSON and call the specified callback.
I tried to return something like JsRaw(compact(render(resultData))).cmd
(which is obviously not the right way to do it), but then the success()
function on the client side is never called (maybe some
Content-Type/Accept issue with JQuery?), instead the AJAX call is
repeated again and again, until the maximum number of retries is
reached.
Since obviously someone introduced the jsContext parameter to jsonCall,
I'd be happy for a bit of guidance on how to use this. Actually, it
seems to me as if only the jsonCall method with the signature
def jsonCall(jsCalcValue: JsExp,
jsonContext: JsonContext,
func: String => JsObj)
is actually usable with a jsonContext, is that right? (Also because
that's a JsonContext, not a JsContext...)
So is it possible at all with the SHtml._ functions to have a function
that calls a callback function from the current context with the
computed JSON data? Any help is much appreciated,
Thanks,
Tobias