Friday, January 29, 2010

RE: JSON View

This is my controller function

function manage_edit($id = null)
{
if ($this->RequestHandler->isAjax()) {
Configure::write('debug', 0);
$award = $this->Award->getAward($id, $this->Auth->user('id'));

if (!empty($award)) {
$this->layout = 'ajax';
if (!empty($this->data)) {
$this->autoLayout = false;
$this->autoRender = false;
$this->Award->id = $id;
$black = array('id', 'profile_id', 'created');
if ($this->Award->save($this->data, true,
array_diff(array_keys($this->Award->schema()), $black))) {
$this->set('award', $this->Award->getAward($id,
$this->Auth->user('id')));
$response['status'] = true;
$response['html'] =
$this->render('/elements/awards/view_block', 'ajax');
$this->header('Content-Type: application/json');
echo json_encode($response);
exit();
} else {
$this->set('errors',
$this->Award->validationErrors);
$this->data = $award;
$response['status'] = false;
$response['html'] =
$this->render('/elements/awards/manage_edit', 'ajax');
$this->header('Content-Type: application/json');
echo json_encode($response);
exit();
}
}
if (empty($this->data)) {
$this->data = $award;

$this->render('/elements/awards/manage_edit', 'ajax');

}
}
} else {

$this->redirect(array('manage' => true, 'action' =>
'index'));
}


$this->set('errors', $this->Award->validationErrors);
}

So this returns noting to the page. I am updating a div and spitting out the
$response['html'] to the specific div which returns only the element. One
thing is the exit(); I think that's what I forgot to mention seems to not
echo out the actual view / element.

Give that a try.

Dave

-----Original Message-----
From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf
Of thomaus
Sent: January-29-10 6:50 AM
To: CakePHP
Subject: Re: JSON View

Hi Dave,

I tried it this way but it's the same.

$this->layout = 'ajax';
$this->autoLayout = false;
$this->autoRender = false;
$response['html'] = $this->render('/form/form_json');

So is there a way to get the result content of a view in a var (in this case
$response['html']) without rendering it (= without getting the annoying
echo) ?

thomas...


On Jan 28, 4:57 pm, "Dave" <make.cake.b...@gmail.com> wrote:
> Hey,
> Simply include
>
> $this->layout = 'ajax';
>           $this->autoLayout = false;
>           $this->autoRender = false;
>
> In the controller action to stop the view from output. All you get is
> the json data then do what you need with it
>
> Dave
>
> -----Original Message-----
> From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On
> Behalf Of thomaus
> Sent: January-28-10 9:08 AM
> To: CakePHP
> Subject: Re: JSON View
>
> Hi there,
>
> I need to render a full HTML view with header, css and js includes and
> pass it as a JSON.
>
> I liked Dave's trick :
>
> $response['html'] = $this->render('my_view_needed'); Then echo back my
> json_encode($response) and use response.html in the js to render the
> view / messages / errors.
>
> But there is a problem, when you do : $response['html'] =
> $this->render ('my_view_needed'); it correctly puts all the HTML view
> content in your $response['html'] var but still it outputs the view
> content just after so the JSON callback format is incorrect. Is there
> a way to get the content of a view render without rendering it?
>
> Otherwise how can I do if I want to send the full HTML content of my
> view as a JSON callback?
>
> Thanks in advance.
>
> On Jan 21, 8:03 am, Akeda Bagus <ad...@gedex.web.id> wrote:
> > On Mon, Jan 11, 2010 at 10:50 PM, Dave <make.cake.b...@gmail.com> wrote:
> > > Does anyone know how to return a JSON object from an ajax request
> > > to return the view and a success status? I see all these tutorials
> > > to retrn data but no html. Everything is layout = false,
> > > autoRender = false. I can return the view in normal html but I
> > > need to get a status = true or false to the js to tell it what to
> > > do next depending on the status. I need to update 1 of 2 possible
> > > divs with the response and that's why I need the success to tell
> > > the js if success
> update this div if no success then do something different.
>
> > > I need to return a view along with my success or fail status. Not
> > > just an error message as I have found in my json / cakephp searching.
>
> > > so in the controller
> > > $this->layout = 'ajax';
> > > if(this->Model->save())
> > > {
> > > //return success = true and success view $respounse = array('status'
> > > => true, 'html' => ???
>
> > > } else {
>
> > >  //return false
> > >  $respounse = array('status' => false); }
> > > $this->header('Content-Type: application/json');
> > >        echo json_encode($response);
>
> > > Ideas?
>
> > My approach is:
> > 1. create json layout in views/layout/json/default.ctp which contains:
> > <?php
> > header("Pragma: no-cache");
> > header("Cache-Control: no-store, no-cache, max-age=0,
> > must-revalidate");
> > header('Content-Type: application/json');
>
> > echo $content_for_layout;
> > ?>
>
> > 2. create common view for json, says app/views/common/jsonize.ctp --
> > my controllers use this oftenly, which contains:
> > <?php echo $javascript->object($result); ?>
>
> > 3. I put jsonize in app_controller.php, so other controllers will
> > benefit
> this:
> > function jsonize() {
> >     $this->layout = 'json/default';
> >     $this->viewPath = "common";
>
> >     if ( Configure::read('debug') > 0 ) {
> >         Configure::write('debug', 0);
> >     }
>
> >     if ( !$this->params['named'] ) {
> >         $this->Session->setFlash('Invalid parameter for JSON',
> > 'error');
> >         $this->redirect(array('action'=>'index'));
> >     }
>
> >     foreach ($this->params['named'] as $field => $value) {
> >         $like = str_replace('*', '%', $value);
> >         $condition[Inflector::singularize($this->name).".$field
> > LIKE"] = $like;
> >     }
> >     $this->{Inflector::singularize($this->name)}->recursive = -1;
> >     $result =
> > $this->{Inflector::singularize($this->name)}->find('all',
> > array( 'conditions' => $condition ));
> >     $this->set('result', $result);
>
> > }
>
> > It just a little modification is you used on $this->Model->save()
>
> > --
> > regards,
> > gedex
>
> > blog:http://gedex.web.id
>
> Check out the new CakePHP Questions sitehttp://cakeqs.organd help
> others with their CakePHP related questions.
>
> You received this message because you are subscribed to the Google
> Groups "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com To
> unsubscribe from this group, send email to
> cake-php+unsubscribe@googlegroups.com For more options, visit this
> cake-php+group athttp://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others
with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com To
unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group
cake-php+at http://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en

No comments: