简体   繁体   English

如何在JSON字符串中包含Javascript字符串构造函数?

[英]How can I include a Javascript string constructor in a JSON string?

I'm building a template to Jquery Mobile listviews. 我正在构建Jquery Mobile列表视图的模板。 To make the template fully generic, I need to pass the listitem text. 为了使模板完全通用,我需要传递listitem文本。 So currently my listview config looks like this: 所以目前我的listview配置看起来像这样:

<ul data-role="listview" data-create="false" class="template" data-config='{
    "type":"listview",
    "data":"getRetailers",
    "custom_classes":["embedded_filter updateResults f-875","nMT pickList f-875 widget_listview","f-n",""],
    "lib":"static_listview.html",
    "tmp":"tmp_listview_inset",
    "lang":"locale_search",
    ...
    }'></ul>

My problem is how to include a Javascript constuctor for the list item text. 我的问题是如何为列表项文本包含Javascript构造函数。 This should be the following: 应该是以下内容:

inv.company+', '+ inv.zip + ', ' + inv.city

But inserting it like this: 但是像这样插入它:

...
"text":"inv.company+', '+ inv.zip + ', ' + inv.city"
}

does not work. 不起作用。

Question : 问题
How to include Javascript constructors in JSON? 如何在JSON中包含Javascript构造函数?

试试这个代码:

"text":"'"+ inv.company+ "', '"+ inv.zip + "','" + inv.city+ "'";

Ok. 好。 Not nice, but working: 不好,但是可以工作:

1) In my JSON I'm only passing a function name like so: 1)在我的JSON中,我只传递了一个函数名称,如下所示:

<ul data-role="listview" data-create="false" class="template" data-config='{  
    "type":"listview",
    "data":"getRetailers",
    "custom_classes":["embedded_filter updateResults f-875","nMT pickList f-875 widget_listview","f-n",""],
    "lib":"static_listview.html",
    "tmp":"tmp_listview_inset",
    "lang":"locale_search",
    "text":"buildRetailers",
    ...
}'></ul>

2) Then in my script I'm adding buildRetailers to my dynoData module: 2)然后在我的脚本中,将buildRetailers添加到我的dynoData模块中:

var dynoData = {
    ...
    buildRetailers: function (inv) {
        return inv.company+', '+ inv.zip + ', '+ inv.city;
    },
    ...
}

3) And call this from my listview string builder: 3)并从我的listview字符串生成器中调用它:

....
listItem = listItem.replace( widget.options.re_theme, li_theme );
listItem = listItem.replace( widget.options.re_iconpos, li_iconpos );
listItem = listItem.replace( widget.options.re_icon, li_icon );
// call the function to return the list item text
listItem = listItem.replace( widget.options.re_text, dynoData[ li_text ](inv) );

So I can't seem to pass the function string in the JSON configuration so I need to add a function to construct what I need. 因此,我似乎无法在JSON配置中传递函数字符串,因此我需要添加一个函数来构造所需的函数。 If someone knows a better way please post. 如果有人知道更好的方法,请发布。 I will test and check. 我将进行测试。

Can't you just pass what the object literal version of the inv object is. 您不能仅传递inv对象的对象文字版本是什么。

"text" : {
    "company": "some value here",
    "zip": "another value here",
    "city": "another value"
}

Normally you would do it like this: 通常,您会这样做:

"text": function(){ return inv.company+', '+ inv.zip + ', ' + inv.city }

But I'm not sure if it ever works. 但我不确定是否能奏效。 You can try it anyways. 您仍然可以尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM