Let me first describe the situation the best I can.
So I am working in a PHP MVC framework. Inside the controller of a method I collect and format the data as it needs to be and I send it off to the view. The view page in turn uses that data but also includes separate javascript script files that also need to access the data the controller is passing to the view.
What is the best way to make sure the data the controller is passing to the view is accessible to the other javascript files?
Right now what I am doing is just creating a bunch of hidden for fields so that in my javascript I can just do:
$('selector').val();
In order to access the data but I can’t help to think that there might be a cleaner way to do this.
The solution I often use in this case is just output data to the view in JSON format inside if the script tag:
So then this data will be available form other scrips as
window.data.In case of complicated application you should consider saving data to the variable in the separated namespace to prevent global scope pollution.