Bad MVC - how to avoid it when using AJAX dropdown? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Bad MVC - how to avoid it when using AJAX dropdown? (/showthread.php?tid=39081) |
Bad MVC - how to avoid it when using AJAX dropdown? - El Forum - 02-28-2011 [eluser]imcl[/eluser] Hello friends, At the risk of exposing my ignorance in CI / AJAX and getting pummeled for what I'm currently doing, can anyone help me with this issue? I have a simple 2-tier dropdown: - in the first dropdown you select your 'plant'. - onChange, this dropdown runs controller /plant/Subtype/ via AJAX. - /plant/Subtype/ has a method that calls /plant_model/Subtype_model - /plant_model/Subtype_model runs a query that fetches all subtypes for that specific plant. - now I know that the right thing to do would be to return $query; and send it back to the controller -- then send it to the view in order to populate a form_dropdown(). - but what I'm doing is echoing straight form the model to the browser: Code: // plant_model/subtype_model/ Before you splatter you screen with projectile puke, understand that I'm doing this because the AJAX method I'm using clears up the DIV where the second dropdown should go. Code: function getPlantSubtype(strURL) { In that case, how would I run a loop through the items from the $query result if they were passed back to the controller, then to the view? If anyone could give me the roadmap, it would be much appreciated! Thanks. Bad MVC - how to avoid it when using AJAX dropdown? - El Forum - 03-01-2011 [eluser]AgustÃn Villalba[/eluser] I recommend you to pass the result from the model to the controller as an array, and after pass it to a view called "ajax_generic_dropdown_view" which receives the array and iterates over it generating the options of the dropdown. Bad MVC - how to avoid it when using AJAX dropdown? - El Forum - 03-01-2011 [eluser]JuanitoDelCielo[/eluser] Every layer should do their job Controller Code: function subcategories () { view Code: <?php foreach( $query->result() as $subcategory ) :?> Finally JS Code: url = 'http://localhost/../'; Bad MVC - how to avoid it when using AJAX dropdown? - El Forum - 03-02-2011 [eluser]imcl[/eluser] OK thanks to both - will try your suggestions |