2013-06-28 16 views
6

https://github.com/kripken/lua.vm.js/issues/5で提起した問題です。私はstackoverflowに提出したいと思います。私はより高い露出を考えると、ここでより速い答えを得るかもしれません。私の質問がはっきりと理解できるように、私はそれを改めて説明します。以下の例からコールバックデータにアクセスするにはどうすればよいですか?lua.vm.js ajaxコールバックが発生しましたが、データが返されません

は問題が提出さ:

ブラウザでJavaScriptを交換するための巨大な可能性を持つソフトウェアの素晴らしい作品(lua.vm.jsがあります)!

メーリングリスト、ウィキ、問題などから収集されたコードの抜粋です。すべてのことが、パフォーマンスに影響を与えることなく認識されています。私は、JQueryのajax呼び出しでコールバックの戻り値に問題があり、WebSocketはメッセージを返しました。例えば

(以下script_example.htmlを参照)

js.run('$.get("/glossary.json", function(data) { console.log(data); });') -- this works 
jq.get("/glossary.json", function(data) print(data) end) -- the callback is firing, but data is not returned 

回避策負荷()関数の使用:以下

jq('#result').hide().load("/glossary.json", function() print(jq('#result').html()) end) -- this works because after the callback is fired, we just collect the result from the result div 

がscript_example.htmlに入る(lua.vm.参照JS gitリポジトリ):

<!-- begin script tag example --> 

<script src="lua.vm.js"></script> 
<script src="jquery-1.10.1.js"></script> 

<!-- 
    Simplest web server for serving static files 
    python -m SimpleHTTPServer 8080 
--> 

<script type="text/lua"> 
-- Print contents of `tbl`, with indentation. 
-- `indent` sets the initial level of indentation. 
function tprint (tbl, indent) 
    if not indent then indent = 0 end 
    for k, v in pairs(tbl) do 
    formatting = string.rep(" ", indent) .. k .. ": " 
    if type(v) == "table" then 
     print(formatting) 
     tprint(v, indent+1) 
    else 
     print(formatting .. tostring(v)) 
    end 
    end 
end 

-- function test() 
-- return 'ok' 
-- end 
-- for i=1,5 do 
-- js.global.alert(test()) 
-- end 

local jq = js.get("$") 
-- jq('body').append("plop").click(function() js.global.alert("plop click") end) 
-- local version = jq().jquery 
-- js.global.alert(version) 
-- jq('#result').load("/glossary.json") 
jq('#result').hide().load("/glossary.json", function() print(jq('#result').html()) end) 
-- jq.get("/glossary.json", function(data) print(data) end) -- callback is firing, but data is not returned 
-- js.run('$.get("/glossary.json", function(data) { console.log(data); });') 

-- local ws = js.new.WebSocket("ws://echo.websocket.org/?encoding=text") 
-- ws.onopen = function() 
-- print("connected!") 
-- ws.send("Rock it with HTML5 WebSocket") 
-- end 
-- ws.onclose = function() 
-- print("disconnected") 
-- end 
-- ws.onerror = function(error) 
-- print(error) 
-- end 
-- ws.onmessage = function(e) 
-- tprint(e) -- using tprint() because an empty table is returned instead of the message 
-- ws.close() 
-- end 
</script> 

<!-- end script tag example --> 

<div id="result"></div> 

上記実施例にロードglossary.jsonファイル:

{ 
    "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" 
      } 
     } 
    } 
    } 
} 

答えて

2

申し訳ありませんが、私は戻ってきて解決策を報告す​​るのを忘れました。

jQueryの1.5以降、すべてのjQueryのAjaxメソッドは、XMLHTTPRequestオブジェクトのスーパーセットを返します。このjQuery XHRオブジェクト、つまり$ .get()によって返された "jqXHR"はPromiseインターフェイスを実装して、すべてのプロパティ、メソッドなどを提供します。

たとえば、responseTextプロパティとresponseXMLプロパティ、 getResponseHeader()メソッド

この、次のコードの動作に基づいて、コールバックデータを返す(responseText):

local jq = js.get("$") 
local jqxhr = jq.get("/glossary.json") 
jqxhr.done(function() print(jqxhr.responseText) end) 

一つは、直接のXMLHttpRequestラッパー関数と完全にjQueryをバイパスすることができ:

local xhr = function(method, url, callback) 
    local xhr = js.new.XMLHttpRequest() 
    xhr.onreadystatechange = function() 
    if xhr.readyState == 4 and xhr.status == 200 then 
     return callback(xhr.responseText) 
    end 
    end 
    xhr.open(method, url) 
    xhr.send() 
end 

xhr("GET", "/glossary.json", function(data) print(data) end) 
関連する問題