For a while i have been using javascript frameworks and libraries to handle views which is greate. Library like knockoutjs and framework like Backbone. Now i am thinking of using it in views when ever i work on any MVC. I have a few suggestions and few questions.
Passing data from controller to view.
After some research i found these three ways to pass data to views. Examples using php and knockoutjs
First
In controller (Codeingiter)
$data['viewData'] = $array;
$this->load->view('some_view_file',$data);
In view
var myData = '<?php echo json_encode($viewData);?>'
self.myObservable(myData);
Then using knockout bind data to view.
Second
<input type="hidden" value="<?php echo json_encode($myData);?>" id="myData">
Then using jquery
var myData = $('#myData').val()
self.myObservable(myData);
Then using knockout bind data to view.
The third (Limited)
Make the data query string and then read the querystring with javascript to pass to knockoutjs.
OK. On page load these are methods to pass data to view instead of running ajax.
What i have in mind?
- No looping or conditioning on view using php/asp
- First page load should fill knockout viewmodel then with ajax, updated data will be easy to implement
Questions?
- Is there more elegent way to pass data to view without reaveling to hacker?
- Any security measures i should follow in this technique?
- On browser with javascript closed, how to handle the application (Do i still need php/asp on views?)