如何通过JSON传递函数名称并在javascript / jQuery中调用它? [英] how to pass a function name via JSON and call it in javascript/jQuery?

查看:251
本文介绍了如何通过JSON传递函数名称并在javascript / jQuery中调用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的JSON如下所示:

  {
type:listview,
//我要调用
content的函数:dynoData.getRetailers (),
custom_classes:[,nMT pickList,,],
lib:static_listview.html,
tmp: tmp_listview_inset,
lang:locale_search,
...

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



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



这就是我想要的:

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

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

问题

如何触发实际功能?

感谢!



编辑

我将JSON字符串放在实际页面上的HTML元素上,我将用我正在构建的元素来取代它。这里是HTML:


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

我可以将所有这些放入 data - 属性,但这会很麻烦......



解决方案
这工作:



1)将JSON更改为:

  ...method:getRetailers,.. 。



<2>来自Javascript的调用:

  content = dynoData [dyn.method](); 

谢谢大家!

解决方案假设这个函数总是 dyn 对象的一部分,你可以使用象下面这样的符号来调用一个函数:

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

因此,如果您可以调整json,您可以发回如下内容:

 content:{mainObject:dynoData,method:getRetailers} 


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

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

作为使用jQuery的示例,请使用以下命令:

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

与以下相同:

  $('div')。hide()


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

My JSON looks like this:

{  
    "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().

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:

<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:

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

2) call from Javascript:

content = dynoData[ dyn.method ]();

Thanks everyone!

解决方案

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

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

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

"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 :

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

Which is the same as :

$('div').hide()

这篇关于如何通过JSON传递函数名称并在javascript / jQuery中调用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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