scala lift 中范围内的 javascript 回调

发布于 2024-12-10 22:33:16 字数 1516 浏览 0 评论 0原文

所以我一直在 Scala 中使用 Lift,并且非常喜欢它。我可能只是错过了 lift javascript 库中存在的一些东西,但我还没有找到任何使用作用域 javascript 回调的方法。处理回调的 lift 方式似乎是将回调作为函数名称传递,并让 lift 返回一个 Call() 函数的 JsCmd。

我的电梯代码很大程度上基于此示例 http://demo.liftweb.net/json_more 我的 javascript 看起来有点像

function operation(config) {
    var actions = config.actions,
        action = actions.shift(),
        name = config.name;

    function chainAction(response) {
        if (actions.length > 0) {
            action = actions.shift();
            action.action(name, chainAction);
        }
    }

    action.action(name, chainAction);
}

operation({
    name: "ajax",
    actions: [
        { action: ajaxCall1 },
        { action: ajaxCall2 }
    ]
});

我想要 ajaxCall1 和 ajaxCall2 来作为要解除的 AJAX 调用。即电梯示例中的 callNoParam() 和 chainAction 是作用域回调。有没有办法在我缺少的电梯中做到这一点?为了清楚起见,我已经能够让这段代码调用 lift 函数,但不能正确处理回调。

谢谢。

编辑

在阅读 lift 生成的 JavaScript 代码后,看起来确实有成功/失败回调的占位符。特别是,看起来这条提升线

AllJsonHandler.is.jsCmd

正在生成

function F86737576748N5SY25(obj) {liftAjax.lift_ajaxHandler('F86737576748N5SY25='+ encodeURIComponent(JSON.stringify(obj)), null,null);}

引用此方法

lift_ajaxHandler: function(theData, theSuccess, theFailure, responseType)

的这行 javascript ,但不允许我传递 theSuccess 或 theFailure,它们看起来像是被传递到 jQuery.ajax() 调用中。我的调查仍在继续。如果有人在 is.jsCmd 上有任何好的资源,我们将不胜感激。

So I've been playing around with Lift in Scala, and I've been enjoying it a lot. I might just be missing something that exists in the lift javascript library, but I haven't been able to find any way of using a scoped javascript callback. It seems that the lift way of handling callbacks is to pass the callback as function name and have lift return a JsCmd that Call()s the function.

My lift code is heavily based on this example http://demo.liftweb.net/json_more
And my javascript looks kinda like

function operation(config) {
    var actions = config.actions,
        action = actions.shift(),
        name = config.name;

    function chainAction(response) {
        if (actions.length > 0) {
            action = actions.shift();
            action.action(name, chainAction);
        }
    }

    action.action(name, chainAction);
}

operation({
    name: "ajax",
    actions: [
        { action: ajaxCall1 },
        { action: ajaxCall2 }
    ]
});

Where I'd want ajaxCall1 and ajaxCall2, to be AJAX calls to lift. i.e. callNoParam() in the lift example, and chainAction to be the scoped callback. Is there a way to do this in lift that I'm missing? For clarity, I have been able to get this code to call the lift function, but not to handle the callback correctly.

Thanks.

Edit

Upon reading through the lift-generated javascript code, it looks like there are indeed placeholders for success/failure callbacks. In particular, it looks like this line of lift

AllJsonHandler.is.jsCmd

is generating this line of javascript

function F86737576748N5SY25(obj) {liftAjax.lift_ajaxHandler('F86737576748N5SY25='+ encodeURIComponent(JSON.stringify(obj)), null,null);}

which references this method

lift_ajaxHandler: function(theData, theSuccess, theFailure, responseType)

But not allowing me to pass theSuccess or theFailure which look like they are being passed along into jQuery.ajax() calls. My investigation continues. If anyone has any good resources on is.jsCmd it would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2024-12-17 22:33:16

下面的一段代码将 Javascript 函数 doCallback 添加到页面(在 #placeholder 中)。该函数将向控制台打印一行,然后向服务器的函数 commandCallback 执行 ajaxCall。

def addExecuteCallback(ns: NodeSeq):NodeSeq = {
  val log = JsRaw("console.log('[doCallback] Generated from Lift.');").cmd &
            SHtml.ajaxCall(JsRaw("commandString"), commandCallback _)._2.cmd
  val f = JsCmds.Function("doCallback", List[String](), log)
  ("#placeholder" #> JsCmds.Script(f)).apply(ns)
}

commandCallback 的末尾,您可以返回:

JsCmds.Run("chainAction('" + valueOfResponse + "');")

Below is a piece of code that adds a Javascript function doCallback to the page (in #placeholder). This function will print a line to the console and then do an ajaxCall back to the server to the function commandCallback.

def addExecuteCallback(ns: NodeSeq):NodeSeq = {
  val log = JsRaw("console.log('[doCallback] Generated from Lift.');").cmd &
            SHtml.ajaxCall(JsRaw("commandString"), commandCallback _)._2.cmd
  val f = JsCmds.Function("doCallback", List[String](), log)
  ("#placeholder" #> JsCmds.Script(f)).apply(ns)
}

At the end of commandCallback, you can return:

JsCmds.Run("chainAction('" + valueOfResponse + "');")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文