如何在metrojs中返回object contain函数
how i return object contain function in meteorjs?
i当前使用metrojs 0.9.2
我想将对象从服务器方法返回到客户端方法调用
在该服务器返回对象中包含一个函数作为值,我认为可以使用metrojs 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":{}}}}}
如何将包含对象的函数作为返回传递?
解决此类问题的正确方法是在客户端定义所有感兴趣的函数,然后根据您传递的EJSONable值选择合适的函数。如果这是应用程序中的常见模式,您可以创建一个可能操作的字典:
Actions = {};
Actions.alertOk = function() {
alert('ok');
};
Actions.confirm = function(message) {
if(confirm(message)) alert('ok');
};
...
然后在您的返回语句中传递操作名称:
return {
...
action: {
name: 'confirm',
arguments: [
'Do you want an OK alert?',
],
}
};
然后在需要时调用请求的操作:
Actions[action.name].apply(this, action.arguments);
您可以在服务器上使用toString,在客户端上使用eval。
//server
var str = (function click() {
alert('ok');
}).toString();
//client
eval(str)();
只要确保您理解使用eval的含义即可。
相关文章:
- 函数未在Object.keys或Object.getOwnPropertyNames下列出,但可以调用
- delete在Object上效率低下,但在DOM Element's的数据属性,与null out相比
- 有没有一种方法可以列出Ember.Object的所有绑定
- 如何取消object.prototypes javascript的一个函数
- 为什么是文档.旧版应用程序中的DOM-object.properties为null
- 将*.js文件的内容放入Object中
- Object.prototype using 'this'
- 使用Object.create()的角度服务继承
- 如何使用object.assign()从其他对象引用基本对象属性
- 循环的数组推入在Object容器中具有不同的值
- reducers在redux中得到Function not Object,what'it’他错了
- JSON.stringify和Object.keys在同一个对象上产生不同的结果
- 在使用object.create创建的对象中使用super
- Join架构验证:Join.object定义数组中的有效键
- jquery打印[object XMLDocument]而不是文件内容
- ExtJS 4 Object.prototype fail
- JavaScript-从对象数组中输出随机OBJECT
- 为什么“{}+1”在Chrome和Firefox中排名第一,而字符串'[object object]1'
- 什么是“;原型;通过JavaScript中的Object Literal Notation创建的对象的链接
- 如何在metrojs中返回object contain函数