jsonCall with jsonContext

117 views
Skip to first unread message

Tobias Pfeiffer

unread,
Oct 29, 2012, 9:02:52 PM10/29/12
to lif...@googlegroups.com
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

Diego Medina

unread,
Oct 29, 2012, 10:46:25 PM10/29/12
to lif...@googlegroups.com
If you don't get an answer in a few days, could you put together a
sample app [1], and I'll try to make it work (that jquery plugin looks
interesting, and I have been meaning to look at how to use jsonContext
too)

[1] https://www.assembla.com/wiki/show/liftweb/Posting_example_code

Thanks

Diego
> --
> --
> Lift, the simply functional web framework: http://liftweb.net
> Code: http://github.com/lift
> Discussion: http://groups.google.com/group/liftweb
> Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code
>
>
>



--
Diego Medina
Lift/Scala Developer
di...@fmpwizard.com
http://www.fmpwizard.com

Jeppe Nejsum Madsen

unread,
Oct 30, 2012, 4:31:04 AM10/30/12
to lif...@googlegroups.com
Tobias Pfeiffer <tob...@tesobe.com> writes:

> 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
I think what yoy're missing is this
https://github.com/lift/framework/issues/1317

I've forgotten about this and will add it asap...

/Jeppe

Tobias Pfeiffer

unread,
Oct 30, 2012, 7:12:08 AM10/30/12
to lif...@googlegroups.com
Hi,

Am Dienstag, 30. Oktober 2012, 09:31:04 schrieb Jeppe Nejsum Madsen:
> I think what yoy're missing is this
> https://github.com/lift/framework/issues/1317
>
> I've forgotten about this and will add it asap...

Great, thanks a lot!

When can I expect this to be in the 2.5-SNAPSHOT jars? Are they built
nightly?

Tobias
signature.asc

Jeppe Nejsum Madsen

unread,
Oct 30, 2012, 8:17:48 AM10/30/12
to lif...@googlegroups.com
It's built on demand and according to
https://lift.ci.cloudbees.com/job/lift-framework-2.9.x/ jars have been
pushed to sonatype.

/Jeppe

Tobias Pfeiffer

unread,
Oct 30, 2012, 6:25:05 PM10/30/12
to lif...@googlegroups.com
Hi again,

Am Dienstag, 30. Oktober 2012, 09:31 schrieb Jeppe Nejsum Madsen:
> I think what yoy're missing is this
> https://github.com/lift/framework/issues/1317
>
> I've forgotten about this and will add it asap...

Perfect, works like a charm! Thank you!

Here's the snippet code to get the select2 widget working with a
server-side lookup:

def ajaxImageSelectFun = {
"script *" #> {
case class SearchQuery(term: String, page: Int)
val handleJson = SHtml.jsonCall(JE.JsRaw("query"), new JsonContext(Full("success"),
Empty), (json: JValue) => {
val result = Extraction.extractOpt[SearchQuery](json)
val pageSize = 3
val results = result match {
case Some(r) =>
val page = r.page
// search for a matching item, take [pageSize+1] many results,
// start at ([page]-1)*[pageSize]
whereeverIGetMyDataFrom.map(i => {
("id" -> i.id.toString) ~ ("text" -> i.text.is)
})
case _ => List()
}
val r: JValue =
("results" -> JArray(results.take(pageSize))) ~
("more" -> (results.size > pageSize))
r
})

JsCmds.Function("selectImg", List("query"), JE.JsRaw(
"""console.log(query);
function success(data, textStatus, jqXHR) { query.callback(data); }; """ +
handleJson.toJsCmd).cmd).toJsCmd
}
}

Tobias
signature.asc

Tobias Pfeiffer

unread,
Nov 3, 2012, 6:23:30 PM11/3/12
to lif...@googlegroups.com
Hi,

Am Dienstag, 30. Oktober 2012, 03:46 schrieb Diego Medina:
> If you don't get an answer in a few days, could you put together a
> sample app [1], and I'll try to make it work (that jquery plugin
> looks interesting, and I have been meaning to look at how to use
> jsonContext too)

I've written a blog post about how to use the select2 library to edit
MongoDB's ObjectIdRefListField with a SHtml.multiSelectObj-style
interface, but using AJAX calls to look up the matching database
entries. The post is at
<https://nablux.net/tgp/weblog/44/>,
happy to receive your feedback.

Tobias
signature.asc

Diego Medina

unread,
Nov 22, 2012, 4:06:30 PM11/22/12
to Lift
Thanks for sharing the post!
Reply all
Reply to author
Forward
0 new messages