简体   繁体   English

在海边用 Ajax 替换图像

[英]Replace an image with Ajax on seaside

I have been working on Seaside 3.1 for a couple of days and I'm trying to design a simple TicTacToe with Ajax.我已经在 Seaside 3.1 上工作了几天,我正在尝试使用 Ajax 设计一个简单的 TicTacToe。

It works great without ajax with this code:使用以下代码在没有 ajax 的情况下效果很好:

renderContentOn: html
html heading: 'Tictactoe'.
html
    form: [ 
        1 to: 3 do: [ :row | 
            1 to: 3 do: [ :col | 
                html imageButton
                    url: (tictactoe imageCase: row * 10 + col);
                    callback: [ tictactoe playsX: row playsY: col ] ].
            html break ] ]

and with ajax it doesn't work at all, though the page doesn't refresh as expected.使用 ajax 它根本不起作用,尽管页面没有按预期刷新。 The imageButton never changes for a simple image with another url.对于带有另一个 url 的简单图像,imageButton 永远不会改变。

The goal is just to swap the imageButton for an image with another url when I click on it.目标只是在我单击时将 imageButton 与另一个 url 交换为图像。

Here's where I am with my code:这是我的代码所在的位置:

renderContentOn: html
html heading: 'Tictactoe'.
1 to: 3 do: [ :row | 
    1 to: 3 do: [ :col | 
        html imageButton
            url: (tictactoe imageCase: row * 10 + col);
            id: 'case' , row asString , col asString;
            onClick:
                    (html scriptaculous updater
                            id: 'case' , row asString , col asString;
                            callback: [ :r | 
                                        tictactoe playsX: row playsY: col.
                                        r render: (html image url: (tictactoe imageCase: row * 10 + col)) ];
                            return: false) ].
    html break ]

I am new and not very good with this technology, therefore I am ready to hear any answer, advice or guidance.我是新手,不太擅长这项技术,因此我准备好听取任何答案、建议或指导。

I would like to thank you in advance, have a good day.我想提前谢谢你,祝你有美好的一天。

My first suggestion would be to drop Scriptaculous and use JQuery instead, the former isn't developed anymore while JQuery is maintained daily and is supported by Seaside as well.我的第一个建议是放弃 Scriptaculous 并使用 JQuery,前者不再开发,而 JQuery 每天都在维护,并且 Seaside 也支持。

renderContentOn: html
    html heading: 'Tictactoe'.
    1 to: 3 do: [ :row | 
        1 to: 3 do: [ :col | 
            html imageButton
                id: 'case' , row asString , col asString;
                url: (tictactoe imageCase: row * 10 + col);
                onClick:
                    (html jQuery ajax
                        callback: [ tictactoe playsX: row playsY: col ];
                        script: [ :s | 
                            s
                                <<
                                    ((html jQuery id: 'case' , row asString , col asString)
                                        replaceWith: [ :h | 
                                            h image url: (tictactoe imageCase: row * 10 + col) ]) ])].
        html break ]

The key is in the onClick: handler, where you pass a JQuery Ajax object that executes a callback on the server and then returns a script (JS) whose content have the instructions to replace an element with certain id (an imageButton with id 'case' , row asString , col asString ) by what is rendered by the replaceWith: render block..关键是在onClick:处理程序中,您在其中传递一个 JQuery Ajax 对象,该对象在服务器上执行回调,然后返回一个脚本 (JS),其内容包含替换具有特定 id 元素的指令(一个带有 id 'case' , row asString , col asString的 imageButton 'case' , row asString , col asString ) 由replaceWith:渲染块呈现的内容..

You can even go a little further and merge both the callback: and script: into a single call as follows:您甚至可以更进一步,将callback:script:合并为一个调用,如下所示:

onClick:
    (html jQuery ajax
        script: [ :s | 
            tictactoe playsX: row playsY: col.
            s << ((html jQuery id: 'case' , row asString , col asString)
                     replaceWith: [ :h | 
                         h image url: (tictactoe imageCase: row * 10 + col) ]) ])].

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM