Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在meteorjs中返回对象包含函数?_Javascript_Json_Object_Meteor - Fatal编程技术网

Javascript 如何在meteorjs中返回对象包含函数?

Javascript 如何在meteorjs中返回对象包含函数?,javascript,json,object,meteor,Javascript,Json,Object,Meteor,我目前正在使用meteorjs 0.9.2 我想将一个对象从服务器方法返回到客户端方法调用 在这里,服务器返回的对象包含一个函数作为值,我认为这可能与meteorjs EJSON有关 下面给出了服务器方法返回对象 return EJSON.stringify({ plotOptions: { series: { stacking: 'normal',

我目前正在使用meteorjs 0.9.2

我想将一个对象从服务器方法返回到客户端方法调用

在这里,服务器返回的对象包含一个函数作为值,我认为这可能与meteorjs EJSON有关

下面给出了服务器方法返回对象

        return EJSON.stringify({

            plotOptions: {
                series: {
                    stacking: 'normal',
                    point: {
                        events: {
                            click: function() {
                                alert('ok');
                            }
                        }
                    }
                }
            },

        });
Meteor.call("highcharts", Session.get("method"), Session.get("taskId"), function(error, object) {
    $("#highcharts #loading").hide();

    if(error) throwError(error.reason);
    else $("#highcharts").highcharts(JSON.parse(object));

    console.log(EJSON.parse(object));
});
{"plotOptions":{"series":{"stacking":"normal","point":{"events":{}}}}}
客户端接收方法如下所示

        return EJSON.stringify({

            plotOptions: {
                series: {
                    stacking: 'normal',
                    point: {
                        events: {
                            click: function() {
                                alert('ok');
                            }
                        }
                    }
                }
            },

        });
Meteor.call("highcharts", Session.get("method"), Session.get("taskId"), function(error, object) {
    $("#highcharts #loading").hide();

    if(error) throwError(error.reason);
    else $("#highcharts").highcharts(JSON.parse(object));

    console.log(EJSON.parse(object));
});
{"plotOptions":{"series":{"stacking":"normal","point":{"events":{}}}}}
但在浏览器控制台日志中,我无法获得该对象元素值作为函数,它显示以下给定的对象

        return EJSON.stringify({

            plotOptions: {
                series: {
                    stacking: 'normal',
                    point: {
                        events: {
                            click: function() {
                                alert('ok');
                            }
                        }
                    }
                }
            },

        });
Meteor.call("highcharts", Session.get("method"), Session.get("taskId"), function(error, object) {
    $("#highcharts #loading").hide();

    if(error) throwError(error.reason);
    else $("#highcharts").highcharts(JSON.parse(object));

    console.log(EJSON.parse(object));
});
{"plotOptions":{"series":{"stacking":"normal","point":{"events":{}}}}}
如何将对象包含函数作为返回传递?

您可以在服务器和客户端上使用

//server
var str = (function click() {
  alert('ok');
}).toString();

//client
eval(str)();

只需确保您了解使用eval的重要性。

解决此问题的正确方法是在客户端定义所有感兴趣的函数,然后根据传递的合理值选择适当的函数。如果这是应用程序中的常见模式,例如,您可以创建可能操作的字典:

Actions = {};

Actions.alertOk = function() {
  alert('ok');
};

Actions.confirm = function(message) {
  if(confirm(message)) alert('ok');
};

...
然后在return语句中传递操作名称:

return {
  ...
  action: {
    name: 'confirm',
    arguments: [
      'Do you want an OK alert?',
    ],
  }
};
然后在需要时调用请求的操作:

Actions[action.name].apply(this, action.arguments);

你不能把函数字符串化Johan-那我怎么传递函数呢?EJSON有可能吗?你没有。传递其他说明您兴趣的值,并重建客户端。