如何通过JSON传递函数名,并用javascript/jQuery调用函数名?

时间:2022-04-09 16:57:38

I have a JSON string which includes a function I need to call.

我有一个JSON字符串,其中包含一个我需要调用的函数。

My JSON looks like this:

我的JSON是这样的:

{  
    "type":"listview",
    // the function I would like to call
    "content":"dynoData.getRetailers()",
    "custom_classes":["","nMT pickList","",""],
    "lib":"static_listview.html",
    "tmp":"tmp_listview_inset",
    "lang":"locale_search",
    ...

I'm using this to assemble a jQuery Mobile listview on the client. To get the dynamic data, I need to call dynoData.getRetailers().

我正在使用它在客户机上组装一个jQuery Mobile listview。要获取动态数据,需要调用dynoData.getRetailers()。

However I'm struggling to make the call :-)

然而,我正在努力打电话:

This is what I'm trying:

这就是我正在尝试的:

var dyn = $.parseJSON( passed_JSON_string ),
    content = dyn.content;

I had hoped calling it would trigger the function but it just returns the function name as a string.

我希望调用它会触发函数,但它只是将函数名作为字符串返回。

Question:
How can trigger the actual function?

问题:如何触发实际的函数?

Thanks!

谢谢!

EDIT:
I'm putting the JSON string on the HTML element on the actual page, which I will replace with the element I'm building. Here is the HTML:

编辑:我将JSON字符串放在实际页面的HTML元素上,我将用我正在构建的元素替换它。HTML:

<ul data-template="true" data-config='{  
    "type":"listview",
    "content":"dynoData.getRetailers()",
    "custom_classes":["","nMT pickList","",""],
    "lib":"static_listview.html",
    "tmp":"tmp_listview_inset",
    "lang":"locale_search",
    "theme":"c",
    "filter":"true"
    }'></ul>

I could put all of these into data- attributes, but that would be messy...

我可以把所有这些都放到数据属性中,但那样会很混乱……

Solution: This worked:

解决办法:这个工作:

1) change JSON to:

1)JSON更改为:

..."method":"getRetailers", ...

2) call from Javascript:

2)从Javascript调用:

content = dynoData[ dyn.method ]();

Thanks everyone!

谢谢大家!

4 个解决方案

#1


5  

Assuming the function is always part of the dyn object you can use notation like following to call a function:

假设函数总是dyn对象的一部分,你可以使用如下的符号来调用一个函数:

dyn['dynoData']['getRetailers']();

So if you are able to adjust json you could send back something like:

如果你能调整json你可以返回如下内容:

"content":{ "mainObject": "dynoData" , "method" :"getRetailers"}

And translate it to your dynamic function using variables:

并使用变量将其转换为动态函数:

  dyn[content.mainObject][content.method]();

As an example using jQuery try using the following :

作为使用jQuery的一个例子,请尝试使用以下方法:

$('div')['hide']();

Which is the same as :

这与:

$('div').hide()

#2


1  

As charlietfl pointed out you can use object notation to call functions. For your case you have to get rid off () and split it, then call it like this;

正如charlietfl所指出的,您可以使用对象表示法来调用函数。对于这种情况,你必须去掉()并将其拆分,然后这样调用;

jQuery(function($) {
    var temp = $('ul').data('config').content.replace(/\(\)/g, '').split('.');
    window[temp[0]][temp[1]]();
});

However this could solve your problem, if you think about future, you have to extend it a little bit. This way even you don't know the depth, you can call it anyway;

然而,这可以解决你的问题,如果你考虑未来,你必须扩展它一点。这样即使你不知道深度,你也可以叫它;

jQuery(function($) {
    var temp = $('ul').data('config').content.replace(/\(\)/g, '').split('.'), func, i, il = temp.length;
    for(i = 0; i < il; i++) {
        if(func == null) {
            func = window[temp[i]];
            continue;
        }
        func = func[temp[i]];
    }
    func();
});

#3


1  

Try ConversationJS. It makes dynamic calls pretty easy and its a great way to decouple your codebase: https://github.com/rhyneandrew/Conversation.JS

ConversationJS试试。它使动态调用变得非常简单,并且它是一种消除代码库的好方法:https://github.com/rhyneandrew/Conversation.JS。

#4


0  

JSON is purely data notation to be passed around so it is easily read and parsed, therefore it has no concept of functions. However, there are other ways of dealing with this and if you are starting to think that that is the only way to deal with your dilemma, then take a step back and examine your design. Instead of using this:

JSON纯粹是要传递的数据表示法,因此很容易读取和解析,因此它没有函数的概念。然而,还有其他的方法来处理这个问题,如果你开始认为这是解决你的困境的唯一方法,那么后退一步,检查一下你的设计。而不是使用:

eval(yourCode);

Try this

试试这个

var tempFun = new Function(yourCode);
tempFun(); 

#1


5  

Assuming the function is always part of the dyn object you can use notation like following to call a function:

假设函数总是dyn对象的一部分,你可以使用如下的符号来调用一个函数:

dyn['dynoData']['getRetailers']();

So if you are able to adjust json you could send back something like:

如果你能调整json你可以返回如下内容:

"content":{ "mainObject": "dynoData" , "method" :"getRetailers"}

And translate it to your dynamic function using variables:

并使用变量将其转换为动态函数:

  dyn[content.mainObject][content.method]();

As an example using jQuery try using the following :

作为使用jQuery的一个例子,请尝试使用以下方法:

$('div')['hide']();

Which is the same as :

这与:

$('div').hide()

#2


1  

As charlietfl pointed out you can use object notation to call functions. For your case you have to get rid off () and split it, then call it like this;

正如charlietfl所指出的,您可以使用对象表示法来调用函数。对于这种情况,你必须去掉()并将其拆分,然后这样调用;

jQuery(function($) {
    var temp = $('ul').data('config').content.replace(/\(\)/g, '').split('.');
    window[temp[0]][temp[1]]();
});

However this could solve your problem, if you think about future, you have to extend it a little bit. This way even you don't know the depth, you can call it anyway;

然而,这可以解决你的问题,如果你考虑未来,你必须扩展它一点。这样即使你不知道深度,你也可以叫它;

jQuery(function($) {
    var temp = $('ul').data('config').content.replace(/\(\)/g, '').split('.'), func, i, il = temp.length;
    for(i = 0; i < il; i++) {
        if(func == null) {
            func = window[temp[i]];
            continue;
        }
        func = func[temp[i]];
    }
    func();
});

#3


1  

Try ConversationJS. It makes dynamic calls pretty easy and its a great way to decouple your codebase: https://github.com/rhyneandrew/Conversation.JS

ConversationJS试试。它使动态调用变得非常简单,并且它是一种消除代码库的好方法:https://github.com/rhyneandrew/Conversation.JS。

#4


0  

JSON is purely data notation to be passed around so it is easily read and parsed, therefore it has no concept of functions. However, there are other ways of dealing with this and if you are starting to think that that is the only way to deal with your dilemma, then take a step back and examine your design. Instead of using this:

JSON纯粹是要传递的数据表示法,因此很容易读取和解析,因此它没有函数的概念。然而,还有其他的方法来处理这个问题,如果你开始认为这是解决你的困境的唯一方法,那么后退一步,检查一下你的设计。而不是使用:

eval(yourCode);

Try this

试试这个

var tempFun = new Function(yourCode);
tempFun();