整个处理JavaScript的参数没有通过 [英] JavaScript Argument not passed across handler

查看:146
本文介绍了整个处理JavaScript的参数没有通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android的谷歌网页视图云频道,但同样的问题以类似的方式使用任何插座时,可能会出现为好。

I am using Google Cloud Channels in Android's WebView, but the same problem probably occurs as well when using any socket in a similar way.

问题:该参数不被处理程序传递,可能是因为函数被称为在不同的范围

Problem: The argument is not passed on by the handler, possibly because functions are called in a different scope.

下面是我的code:

<html>
    <head>
        <script src='{{ channelurl }}jsapi'></script>
    </head>
    <body>
        <script type="text/javascript">

            onMessage = function(message) {

            };

            var token = '{{ token }}';
            var channel = new goog.appengine.Channel(token);

            var handler = {
                'onmessage': onMessage,
            };

            var socket = channel.open(handler);

            socket.onmessage = onMessage;

        </script>
    </body>
</html>

的onMessage只有一个参数(字符串)和我的onMessage功能正常调用,但说法是未定义,可能是因为它是在不同的范围。

onMessage has a single argument (string) and my onMessage function is properly called, but the argument is 'undefined', probably because it is in a different scope.

此问题可能是这些或其他类似的问题重复了,我尝试应用给予那里的配方,但没有成功。

This question might be a duplicate of these or other similar questions, and I tried to apply the recipes given there, but no success.

如何传递参数给事件处理程序?
<一href=\"http://stackoverflow.com/questions/16404327/how-to-pass-event-as-argument-to-an-inline-event-handler-in-javascript\">How通过事件作为参数在JavaScript中内嵌的事件处理程序?

其实,我从这里应用了code

I actually applied the code from here

http://2cor214.blogspot.com/2010/08/passing-arguments-to-event-handler-in.html

和试图用这样的东西打:

and tried to play with things like this:

socket.onmessage =(函数(消息){返回的onMessage})(消息)

socket.onmessage = (function (message) {return onMessage})(message)

在许多变种,但无法得到它的工作。

in many variants, but couldn't get it to work.

我承认我没有正常发育JavaScript和我完全不明白JavaScript并在这里,但在我看来,该参数需要提取莫名其妙包裹的功能。

I admit I am not normally developing JavaScript and I don't exactly understand what JavaScript does here, but it seems to me that the argument needs to be extracted the function wrapped somehow.

任何人都可以阐明请。

-

我打消了我的code部分为简洁。

I removed parts of my code for brevity.

推荐答案

这个问题不是一个JavaScript的问题,因为我承担。这将与任何套接字类型发生同样问题的假设是不正确也是

The problem was not a JavaScript issue, as I assumed. The assumption that the same issue would occur with any socket type was also incorrect.

问题有超过在的onMessage()

The problem had to do with how Google Cloud Channels hands over the message in onMessage().

由于我可以从code,谷歌发布了他们这里井字例子的 HTTP://$c$c.google.com/p/channel-tac-toe/source/browse/trunk/index.html ,行175ff,他们交出的字符串与变量数据的事件,而是叫参数M(如消息)。

As I could see from code that Google posted for their Tic Tac Toe example here http://code.google.com/p/channel-tac-toe/source/browse/trunk/index.html, line 175ff, they hand over the string as an event with the variable "data", but called the argument "m" (as in message).

  onOpened = function() {
     sendMessage('/opened');
  };

  onMessage = function(m) {
     newState = JSON.parse(m.data);
     state.board = newState.board || state.board;
     state.userX = newState.userX || state.userX;
     state.userO = newState.userO || state.userO;
     state.moveX = newState.moveX;
     state.winner = newState.winner || "";
     state.winningBoard = newState.winningBoard || "";
     updateGame();
  }

  openChannel = function() {
     var token = '{{ token }}';
     var channel = new goog.appengine.Channel(token);
     var handler = {
       'onopen': onOpened,
       'onmessage': onMessage,
       'onerror': function() {},
       'onclose': function() {}
     };

     var socket = channel.open(handler);
     socket.onopen = onOpened;
     socket.onmessage = onMessage;
   } 

混淆种类和无证这里 https://cloud.google .COM / AppEngine上/文档/ JAVA /渠道/?CSW = 1 这里的 https://cloud.google.com/appengine/docs/java/channel/javascript ,但是当我改变了消息的签名

Kind of confusing and undocumented here https://cloud.google.com/appengine/docs/java/channel/?csw=1 and here https://cloud.google.com/appengine/docs/java/channel/javascript, but when I changed the message signature to

        onMessage = function(message) {
            ChannelListener.onMessage(message.data); // message
        };

它的工作完美的罚款。

it worked perfectly fine.

这篇关于整个处理JavaScript的参数没有通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆