ajlisowski Posted December 30, 2010 Share Posted December 30, 2010 Hey all. I have a quick question about what the best way to implement something while maintaining a good MVC design. Basically I have a page that has two grids of data. I want these two grids to be sortable and paginated individually. I want to use ajax to redraw these tables when they are re-sorted/paged. Within the MVC design how should I handle this? Should I make an action for each of those grids under a controller and then have my javascript hit that action and grab the output? Obviously I need to originally draw these grids initially from the page. So how do I avoid writing redundent code? Can display the results of other actions within an action? So my main page would display the results of grid1 and grid2. Meanwhile id have ajax calls to grid1 and grid2 whenever they are paginated/resorted? Does this make sense? I get MVC a decent amount but I always get confused when it comes to dynamic pages/ajax within it. Specifically I am using ZF for this project. But this is more of a generic question. Quote Link to comment https://forums.phpfreaks.com/topic/223024-cant-seem-to-wrap-my-brain-around-mvc-with-ajax/ Share on other sites More sharing options...
ignace Posted December 30, 2010 Share Posted December 30, 2010 Take a look at AjaxContext Quote Link to comment https://forums.phpfreaks.com/topic/223024-cant-seem-to-wrap-my-brain-around-mvc-with-ajax/#findComment-1153097 Share on other sites More sharing options...
lazylodr Posted January 25, 2011 Share Posted January 25, 2011 Within the MVC design how should I handle this? Should I make an action for each of those grids under a controller and then have my javascript hit that action and grab the output? Yeah, that seems to make sense. Obviously I need to originally draw these grids initially from the page. So how do I avoid writing redundent code? Can display the results of other actions within an action? So my main page would display the results of grid1 and grid2. Meanwhile id have ajax calls to grid1 and grid2 whenever they are paginated/resorted? I would separate each grid into it's own view script and use partials the first page load. For the ajax actions, I would use the viewRenderer to render the individual grid view scripts, and you'll probably want to disable the layout, if you're using one. Also it would be important that whatever is getting the data for the grids is in a reusable model so you can consistently get the results you need either context (first page load or ajax). If you do all of this: the logic to get the data for the grids is only done once, the views for each grid is only done once, the "first page" is only done once, and your controller just manages these components. In MVC it's important to keep the controllers skinny ... that will naturally promote the most code re-usability. Quote Link to comment https://forums.phpfreaks.com/topic/223024-cant-seem-to-wrap-my-brain-around-mvc-with-ajax/#findComment-1164857 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.