Re: Using render() with Ajax (Cake 1.3.8)
On Thu, Apr 14, 2011 at 2:02 PM, Will <000w.s.s@gmail.com> wrote: > Thanks for the response! Unfortunately that's not quite what I was > looking for. What I'm trying to do is essentially display the > contents of a view as a result of an Ajax request rather than send > JSON data as a result of a controller function (which is what the blog > article was talking about). My solution for now is basically to > output the contents of the view and then append them to a div using > jQuery. I was apparently misunderstanding what the 'ajax' parameter > was supposed to do. If anyone knows of a cleaner way of doing this, > I'm all ears, but my solution appears to fit my needs for now. If you want to fetch HTML content through AJAX then you must append it to something on the page. It doesn't get much cleaner than $('#content').append(data). Although, if you want to replace some existing content that's another story. But it's still a simple job with jquery. There's also the load() function. And, as Krissy said, use an ajax layout so the view doesn't come wrapped in a regular layout. -- 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
RE: Using render() with Ajax (Cake 1.3.8)
$this->layout = 'ajax';//no actual layout will be rendered just the element Or $this->render('path/to/element/view', 'ajax'); //2nd param says use ajax layout If you look the views/layouts/ajax.ctp all it is is so its nothing but the data / html you send it HTH K -Original Message- From: cake-php@googlegroups.com [mailto:cake-php@googlegroups.com] On Behalf Of Will Sent: Thursday, April 14, 2011 3:32 PM To: CakePHP Subject: Re: Using render() with Ajax (Cake 1.3.8) Thanks for the response! Unfortunately that's not quite what I was looking for. What I'm trying to do is essentially display the contents of a view as a result of an Ajax request rather than send JSON data as a result of a controller function (which is what the blog article was talking about). My solution for now is basically to output the contents of the view and then append them to a div using jQuery. I was apparently misunderstanding what the 'ajax' parameter was supposed to do. If anyone knows of a cleaner way of doing this, I'm all ears, but my solution appears to fit my needs for now. On Apr 14, 7:33 am, thatsgreat2345 wrote: > Do thishttp://www.sanisoft.com/blog/2010/10/25/cakephp-sending-json-data-in-... > > On Apr 13, 11:29 am, Will <000w.s.s@gmail.com> wrote: > > > > > > > > > Hi all, > > > I'm brand new to CakePHP, so forgive me if this is a silly question, > > but I have been having trouble understanding how render() is supposed > > to work with Ajax calls. Right now I am trying to implement a > > calendar that has a load bar that disappears once all the scheduling > > data for a particular group of employees is gathered from a somewhat > > slow RESTful API. I am using jQuery to send a post request to my > > controller like this: > > > $(document).ready(function(){ > > var id = $('#id").val(); > > $.post('/employees/month_view', {empid: id}, function(data){ > > $('#loading').fadeOut(); > > }); > > > }) > > > In my controller, I have a function called month_view() that looks > > like this: > > > function month_view(){ > > $this->autoRender = false; > > $empid = $this->params['form']['empid']; > > $conditions= array('empid' => $empid); > > $employees = $this->Employee->find('all', compact('conditions')); > > $employees = array(); > > $this->set('employees', $employees); > > $this->render('/employees/month_view'); > > > } > > > I looked around and found a book ("Beginning Cake PHP: From Novice to > > Professional") and a blog article (http://www.reversefolds.com/ > > articles/show/ajax) that seems to suggest that I should be able to > > call $this->render('/employees/month_view', 'ajax') and have the month > > view render on the page. However, that does not seem to work for me > > and both of those sources look somewhat dated. Right now it appears > > that the month_view.ctp is just being output as if I were calling echo > > on the view, so if I changed my JavaScript to: > > > $(document).ready(function(){ > > ... > > $.post('/employees/month_view', {empid: id}, function(data){ > > $('#loading').fadeOut($('#content').append(data)); > > }); > > > }) > > > I can append the template to the content div, but that feels like the > > wrong way of doing things. My question is: can I call render() for a > > controller function that's being accessed via Ajax and have it display > > the view directly or do I have to use JavaScript to append it (like > > the above)? -- 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php -- 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: Using render() with Ajax (Cake 1.3.8)
Thanks for the response! Unfortunately that's not quite what I was looking for. What I'm trying to do is essentially display the contents of a view as a result of an Ajax request rather than send JSON data as a result of a controller function (which is what the blog article was talking about). My solution for now is basically to output the contents of the view and then append them to a div using jQuery. I was apparently misunderstanding what the 'ajax' parameter was supposed to do. If anyone knows of a cleaner way of doing this, I'm all ears, but my solution appears to fit my needs for now. On Apr 14, 7:33 am, thatsgreat2345 wrote: > Do > thishttp://www.sanisoft.com/blog/2010/10/25/cakephp-sending-json-data-in-... > > On Apr 13, 11:29 am, Will <000w.s.s@gmail.com> wrote: > > > > > > > > > Hi all, > > > I'm brand new to CakePHP, so forgive me if this is a silly question, > > but I have been having trouble understanding how render() is supposed > > to work with Ajax calls. Right now I am trying to implement a > > calendar that has a load bar that disappears once all the scheduling > > data for a particular group of employees is gathered from a somewhat > > slow RESTful API. I am using jQuery to send a post request to my > > controller like this: > > > $(document).ready(function(){ > > var id = $('#id").val(); > > $.post('/employees/month_view', {empid: id}, function(data){ > > $('#loading').fadeOut(); > > }); > > > }) > > > In my controller, I have a function called month_view() that looks > > like this: > > > function month_view(){ > > $this->autoRender = false; > > $empid = $this->params['form']['empid']; > > $conditions= array('empid' => $empid); > > $employees = $this->Employee->find('all', compact('conditions')); > > $employees = array(); > > $this->set('employees', $employees); > > $this->render('/employees/month_view'); > > > } > > > I looked around and found a book ("Beginning Cake PHP: From Novice to > > Professional") and a blog article (http://www.reversefolds.com/ > > articles/show/ajax) that seems to suggest that I should be able to > > call $this->render('/employees/month_view', 'ajax') and have the month > > view render on the page. However, that does not seem to work for me > > and both of those sources look somewhat dated. Right now it appears > > that the month_view.ctp is just being output as if I were calling echo > > on the view, so if I changed my JavaScript to: > > > $(document).ready(function(){ > > ... > > $.post('/employees/month_view', {empid: id}, function(data){ > > $('#loading').fadeOut($('#content').append(data)); > > }); > > > }) > > > I can append the template to the content div, but that feels like the > > wrong way of doing things. My question is: can I call render() for a > > controller function that's being accessed via Ajax and have it display > > the view directly or do I have to use JavaScript to append it (like > > the above)? -- 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: Using render() with Ajax (Cake 1.3.8)
Do this http://www.sanisoft.com/blog/2010/10/25/cakephp-sending-json-data-in-response/ On Apr 13, 11:29 am, Will <000w.s.s@gmail.com> wrote: > Hi all, > > I'm brand new to CakePHP, so forgive me if this is a silly question, > but I have been having trouble understanding how render() is supposed > to work with Ajax calls. Right now I am trying to implement a > calendar that has a load bar that disappears once all the scheduling > data for a particular group of employees is gathered from a somewhat > slow RESTful API. I am using jQuery to send a post request to my > controller like this: > > $(document).ready(function(){ > var id = $('#id").val(); > $.post('/employees/month_view', {empid: id}, function(data){ > $('#loading').fadeOut(); > }); > > }) > > In my controller, I have a function called month_view() that looks > like this: > > function month_view(){ > $this->autoRender = false; > $empid = $this->params['form']['empid']; > $conditions= array('empid' => $empid); > $employees = $this->Employee->find('all', compact('conditions')); > $employees = array(); > $this->set('employees', $employees); > $this->render('/employees/month_view'); > > } > > I looked around and found a book ("Beginning Cake PHP: From Novice to > Professional") and a blog article (http://www.reversefolds.com/ > articles/show/ajax) that seems to suggest that I should be able to > call $this->render('/employees/month_view', 'ajax') and have the month > view render on the page. However, that does not seem to work for me > and both of those sources look somewhat dated. Right now it appears > that the month_view.ctp is just being output as if I were calling echo > on the view, so if I changed my JavaScript to: > > $(document).ready(function(){ > ... > $.post('/employees/month_view', {empid: id}, function(data){ > $('#loading').fadeOut($('#content').append(data)); > }); > > }) > > I can append the template to the content div, but that feels like the > wrong way of doing things. My question is: can I call render() for a > controller function that's being accessed via Ajax and have it display > the view directly or do I have to use JavaScript to append it (like > the above)? -- 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php