So I know that you can create a function on the server than can be invoked
using the following pattern
val fname = Helpers.nextFuncName
val func = () => {
// do something.
}
S.addFunctionMap(fname, func)
You can then invoke the function using http://url?<fname>
Now, what is the pattern if I want the function to return a LiftResponse? I
have the following but the actual LiftResponse is not what is returned.
val imageFuncName = Helpers.nextFuncName
val imageUpload: () => Box[LiftResponse] = () => for {
req <- S.request
image <- req.uploadedFiles.headOption
fileName = UploadManager.store(image)
_ = user.avatar(fileName).save // TODO: Delete the old avatar if it exists.
} yield PlainTextResponse(fileName)
S.addFunctionMap(imageFuncName, imageUpload)
Pete
> --
> 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
> You could use ResponseShortcutException.shortcutResponse from your
> function.
Using ResponseShortcutException I have
--
val imageFuncName = Helpers.nextFuncName
val imageUpload: () => ResponseShortcutException = () => {
val name = for {
req <- S.request
image <- req.uploadedFiles.headOption
fileName = UploadManager.store(image)
_ = user.avatar(fileName).save // TODO: Delete the old avatar if it exists.
} yield fileName
ResponseShortcutException.shortcutResponse(PlainTextResponse(name openOr "error"))
}
S.addFunctionMap(imageFuncName, imageUpload)
--
Using doesn't change anything. Maybe it's relevant to mention I invoke the function
from the client through ajax:
--
var xhr = new XMLHttpRequest();
xhr.open('POST', "?"+post_url);
xhr.onload = function() {
// do something with the data returned
};
xhr.send(formData);
--
where post_url is the name of the function generated by Lift.
> (You might want to use S.fmapFunc too [which is basically
> just a convenience method over the code above]).
The thing is I need the name later so S.fmapFunc doesn't quite cut it :)
Pete