CakePHP - Render views using elements vs ajax -


If views from different controllers use the same element, then different administrators can pass the same data Needed (so maybe doing the same processing) For ideas, would not it be better to call an administrator an AJAX call?

Let's say that we have:

  • Model / Post.php
  • Model / User.php
  • Model / service.
  • Controller / UsersController.php
  • Controller / ServicesController.php
  • View / User / View.CIP
  • View / Service / View.contact
  • view / element / list_users_post.ctp

A user is related to a service, and the user has many posts view / service / In view.cap , I want to display a list of each user of a particular service, and for each user, some related Infos and their 10 List of Ntim positions. In view / user / view.ctp , I want to display user related Infos and post 10 posts of it. Element view / element / list_users_post.ctp allows me to factorize the code displaying a user's table of positions. To set it, var needs to be of $ user.postList , and it should be $ this-> Post- & gt; Search ('All', array ('conditions' = & gt; array ('user_id' => $ userId))) . Therefore, in my UsersController :: view ($ userId) and ServicesController :: view ($ serviceId) operations, I will retrieve the user's posts with some duplicate code ends.

I thought of refactoring code, so do not search any action on ServicesController :: view ($ serviceId) post model, but instead, the view Services / Indexing .ctp causes the AZX call to each user to make the action UsersController :: view ($ userId) In this way, no other duplicated code, but AJAX calls With overhead

Any thoughts?

A good way to avoid duplicate code in your case is to move it onto the model layer Instead in the controller:

  $ this-> Post- & gt; Find ('all', array ('conditions' => gt; array ('user_id' = & gt; $ UserId)))  

You will have the controllers in:

  $ this-> Post- & gt; GetUserPosts ($ userId);  

and in the Post model:

  function getUserPosts ($ userId) {return $ this-> Search ('All', array ('conditions' => array ('user_id' => $ userId))); }  

AJAX calls are also a good solution, but with them, it would be best to keep business logic in the model if possible.

Other ways to avoid duplicates are included in the code (but are not limited to): Useful in view using components in the controllers using behaviors in the model

< P> You should start with keeping your code drill.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -