Jump to content

Cant seem to wrap my brain around MVC with ajax...


ajlisowski

Recommended Posts

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.

  • 4 weeks later...

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.