Reputation: 2643
This question is a little n00b-ish, but I haven't been able to find a satisfactory solution in the CakePHP docs or via google.
I'm trying to make a simple AJAX form that will update some information in a database, and also display that information on the page. The whole process seems straightforward enough, except I can't figure out how to pre-populate the update div.
In my view code, I have
<div id="interestingContent"></div>
<?
echo $this->Form->create();
// create form fields here
echo $this->Js->submit('Save', array('update'=>'#interestingContent');
echo $this->Form-end();
echo $this->Js->writeBuffer();
?>
This works great, and when the form is submitted, my controller correctly calls
$this->render('ajaxview','ajax')
and updates the #interestingContent
div appropriately.
So, the question: What's the best way to pre-populate #interestingContent
from the database? Of course I could just repeat the code from ajaxview.ctp
inside #interestingContent
, but it seems there must be a DRY way to do it.
My first thought was to make ajaxview.ctp
an element, since I'm basically trying to embed one view inside another. That seems hackish to me, though, since my controller would then be rendering an element's view directly. I also considered the new view blocks in 2.1/2.2 but that doesn't seem right either.
I'm certain this sort of thing comes up all the time. Is there a generally accepted method of doing this?
Upvotes: 1
Views: 1007
Reputation:
I had the same problem. It may be hackish, but using an element wil do the job. First, create an element for representing your bd content. In your form view, you use the element. And you create another view that will contain only the element. This view will be the response to your ajax request. By using an element, you prevent code duplication.
Upvotes: 1