Reputation: 18343
I have web site which in turn has few pages with rich User Experience.
In order to populate JavaScript data, I 'render' text into html page in the following way:
<script>
var _data = [
new BigDataObject(22022,{}),
new BigDataObject(30007,
{
"M":new ChildObject(25235,2,296),
"D":new ChildObject(25235,1,296),
"E":new ChildObject(25235,4,148)
})
];
... use _data object in JS functions
</script>
Things work well.
But I also need to transfer data from backend via AJAX call-backs preferably using the same 'strings' to pass data. The problem is that I don't know how to properly parse object back.
Problem #1: I could transfer strings the same way and parse convert them into objects using eval(), but I heard that 'eval' usage is insecure and should be avoided..
Problem #2: I cannot use JSON.parse because the string I've written above is not valid JSON string.
To resolve this I could change the way I generate data and use JSON data instead of imperative instructions.
Problem #3: But in this case I will need to make properties of both BigDataObject and ChildObject to be public. Which I would like to avoid.
Question: How to implement data transferring so I can hide 'setters' for objects properties and use 'safe' approach
Upvotes: 0
Views: 72
Reputation: 1219
I would do a custom object for the response, not using your BigDataObject
or ChildObject
.
This way you could generate an response to protect your actual model, but at the same time generate a proper json
object so you can use jQuery.parseJSON()
function.
Upvotes: 1