What I am doing with the helper, is that I was finding that I was repeating code in multiple views or several times in a single view to get ready to call an element. So, I just moved this common code from the view to a helper, then made a single to call to the helper.
In Controller
public $helpers = array ('Html', 'Form', 'Session', 'Address');
In view
$title = "Shipping Address";
<? php echo $this->Address->makeAddressElements($organization); ?>
In Helper
public function makeAddressElements($organization) {
// some details removed....
$addresses = $this->requestAction('addresses/getByOrg/'.$organization['id']);
foreach ($addresses as $address) {
$title = "Not Used Address";
if ($organization['ship_address_id'] == $address['Address']['id'])
$title = "Shipping Address";
if ($organization['bill_address_id'] == $address['Address']['id'])
$title = "Billing Address";
$html .= $this->_View->element('address', array("addr" => $address['Address'], "title" => $title));
}
return $html;
}
In the Helper in order to call an element that is associated with a view, you need to use the call $this->_View->element(). This will used the attached view to help render the element. This is not well documented, but seems to be the way other helpers do this.
You may not need to do this since you are doing Ajax. You may be able to load all this stuff in the beginning and have javascript hide and display the details as needed.
As for the controller function mapping to views, while Cakephp will defualt create add / index / edit / view functions and views, you are not required to use them all. If you want to have everything displayed and managed for the controller thru the view function and the view view, I don't think this is a problem. You could then use requestAction, or a bunch of set commands to pass information to the view.
This is where my Ajax knowledge puts me on thin ice, but I think you can create Ajax or javascript functions that can call other functions in the controller, like add or edit, and return information that you updated a portion of the screen, so that you don't have to render the entire page.
I guess the way I would look at this is that the view is the webpage. If you want everything to be processed in a single page, with pop outs, etc. then you only need one view. It may be complicated, but if you are familiar doing this with just PHP and Ajax then it should be fine. The controller is where all the business logic is run. So this can be organized based on how you want to process the data.
Not sure if I am getting to basic here, or if this answers your question about views.
Bill
On 4/18/2012 7:02 AM, Timo [via CakePHP] wrote:
Thanks a lot for your answer, it helped me a lot!
I see how elements can be used for what I am trying to do. I tried the simple example from the link you provided, so now I can populate multiple ares of one page with different views - that's great!
What I don't quite grasp is the usefulness of adding a helper as you mentioned in your last paragraph. So basically the helper would get all the data for my elements instead of me calling requestAction multiple times?
I was told that AJAX can be used to put content into element areas (via requestAction, too?), e.g. I have my slideout visible which shows a data entry, then, with my table still visible in the main content area, I choose edit on a different entry and instead of loading the page anew, the slideout element loads new content. How would one go about this task? A simple example would be enough to get me started, since I cannot find any useful ones, e.g. a button that triggers an element to be filled with a different view. I am aware of the .load function in JQuery ( http://api.jquery.com/load/ ), but that does not explain how to include such functionality in a CakePhp environment. Can I simply use something like 'controllername/add' as url parameter for load?
As I see it, one issue remains despite the element solution (which might result from my limited Cake knowledge) - concrete example: The application has tables for companies, users and posts, each table and corresponding controller has index / add / view(show the data) / edit views. Index is displayed in the main area, the other views are supposed to be displayed in the slideout.Would I not need many different element specifications dependinga) on the chosen tableb) the chosen option (add / edit / etc) ?
Is there a way to put for example the rendered view of "View/Company/add.ctp" into an element? Because as far as I understand requestAction, I can only get the data provided by the controller (which makes sense due to the MVC pattern) and this data still has to be echoed and styled. And that is the problem since there is not a common style for add / edit / view (add and edit would have inputs for data values, view would use labels). My thought would be to put those options into one element and control which style is used by a parameter given to the element, I just want to know if that is viable or if there is another, better way to do this.
I hope that was somewhat intelligible :)Many thanks in advance for any helpful advice
Timo--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
To unsubscribe from this group, send email to
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
If you reply to this email, your message will be added to the discussion below:http://cakephp.1045679.n5.nabble.com/Some-Issues-multiple-views-on-one-page-and-help-with-javascript-tp5646347p5648786.htmlTo start a new topic under CakePHP, email [hidden email]
To unsubscribe from CakePHP, click here.
NAML
View this message in context: Re: Some Issues: multiple views on one page and help with javascript
Sent from the CakePHP mailing list archive at Nabble.com.
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
No comments:
Post a Comment