How to include Javascript string constructor in JSON string?

I am creating a template for jQuery Mobile mailing lists. To make the template completely universal, I need to pass the text to listitem. So currently my listview configuration looks like this:

<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","fn",""], "lib":"static_listview.html", "tmp":"tmp_listview_inset", "lang":"locale_search", ... }'></ul> 

My problem is how to enable the Javascript constructor for the text of the list item. It should be the following:

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

But insert it like this:

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

does not work.

Question :
How to include Javascript constructors in JSON?

+4
source share
4 answers

Ok I do not like it, but it works:

1) In my JSON, I only pass the function name:

 <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","fn",""], "lib":"static_listview.html", "tmp":"tmp_listview_inset", "lang":"locale_search", "text":"buildRetailers", ... }'></ul> 

2) Then in my script I add buildRetailers to my dynoData module:

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

3) And call it from my listview row builder:

 .... 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) ); 

Therefore, I cannot pass a function string in the JSON configuration, so I need to add a function to build what I need. If someone knows the best way, write. I will check and verify.

0
source

Try this code:

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

Can't you just pass the object literal version of the inv object.

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

Usually you do it like this:

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

But I'm not sure if it will ever work. You can try anyway.

-1
source

All Articles