daurnimator / lua.vm.js

The project is superceded by Fengari. See https://fengari.io/
MIT License
835 stars 101 forks source link

Unable to call jQuery(document).ready() from Lua #15

Closed ghost closed 10 years ago

ghost commented 10 years ago

Hello,

First of all, let me tell you that this project is super exciting! I am working on server-side Lua proramming (LSP), but being able to write client-side scripting in Lua without any pre-compilation (unlike the Moonshine project) is a big plus! Please keep maintaining and improving this project. It has a tremendous amount of potential, as porting Lua to essentially all web browsers opens up a world of possibilities!

I was testing whether I could re-use jQuery without having to re-write it in Lua completely. Using the $ variable doesn't work, probably because this is an invalid variable name in Lua. Luckily, jQuery provides a global variable called jQuery, so simple things like js.global.jQuery("#divid").html("This is JQuery from Lua!!!") work fine. However, js.global.jQuery(js.global.document).ready(function() ... end) doesn't work, giving a cryptic "SyntaxError" error on lua.vm.js:6624.

I suspect this maybe because I'm trying to pass a Lua function object to a JavaScript function that expects a JS function object, and this conversion may not be supported. Any thoughts?

Regards,

Gabriel Mongefranco Software Developer http://gabriel.mongefranco.com

daurnimator commented 10 years ago

Remember to use the method call syntax (:) when calling a javascript function from lua.

window:jQuery('body'):html("empty")

Otherwise, support for sending lua functions to js is a newly added feature, can you give your code a try again? If it still doesn't work, can you post a small self contained test case (e.g. using jsfiddle)

antonh commented 10 years ago

Hey @gabrielinux, you may want to check a couple issue threads related to JQuery (ajax, callbacks, json, etc.): https://github.com/kripken/lua.vm.js/issues/5 and https://github.com/kripken/lua.vm.js/issues/6

After @daurnimator last commits the following code is now working:

<script type="text/lua">
    local jq = window.jQuery()
    print("version", jq.jquery)
    print("version second call", jq.jquery)
    local jqxhr = window.jQuery:get("/glossary.json")
    jqxhr:done(function() print(jqxhr.responseText) end)
</script>

To avoid 'Origin null is not allowed by Access-Control-Allow-Origin.' when using ajax with local resources, Python's simple http server comes in handy:

python -m SimpleHTTPServer 8080

Then point your browser to http://127.0.0.1:8080/your_example.html

P.S. The glossary.json file loaded in the example above:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}
joelpinheiro commented 8 years ago

Is it possible to call from NodeJS?

Certain with a different approach from window.jQuery.