You can do all of this with requestHandler component ( to check if an action
is 'requested' by another view or element, router::parseExtensions ( to
manage automatic template switching for requests mapped with a ".js" file
extension ); view::requestAction ( to actually request the json ) and the
javascript helper ( check out the inline parameter on the $javascript->link
function and the json encoding and json decoding function also )

I generally set up the router to handle .xml and .js requests. To use much
of ext you need to return json a lot. I then map the js layouts and /js view
subfolders to certain actions. In those actions I use requestAction to grab
the data I want from whatever controller - there is an excellent bakery
article by gwoo on reusable elements. read that if you havent. -
http://bakery.cakephp.org/articles/view/creating-reusable-elements-with-requestaction
- You can request the action complete with its mapped extension following
any parameters you might have even. IE /users/profile/1.js

If I need the js for certain objects ALL THE TIME I set an element up that
uses requestAction if no data is passed to it or my passed data instead to
request the JSON I need. I might even be inclined to put these reusable
parameterized json elements in their own
elements/json/controller_name/action_name folders.

Using the $plugin variable in the various cache functions ( this is usually
blank - resulting in double underscore cache filenames ) you can even cache
the results of each requestAction with more granularity ( a few more bakery
reads -
http://bakery.cakephp.org/articles/view/optimizing-your-cakephp-elements-and-views-with-caching
- and -
http://bakery.cakephp.org/articles/view/cache-elements-individually-for-each-user
)

There is a ton you can do with requestAction and the router alone. Let me
know if you have questions or need samples on implementation.



[EMAIL PROTECTED] wrote:
> 
> 
> Hey Tane - check out cake's JavascriptHelper::object()
> 
> On Nov 1, 6:00 pm, "Tane Piper" <[EMAIL PROTECTED]>
> wrote:
>> Well, what would you know!  Having written out what I wanted to get,
>> and by simply applying a little brainpower, I came up with the
>> solution.  What I did was write a CakePHP component and helper.  First
>> off, here is the component:
>>
>> <?php
>> class JsonComponent extends Component {
>>         var $json_for_layout = array();
>>         function addToJSON($add = array()){
>>                 $this->json_for_layout =
>> array_merge($this->json_for_layout, $add);
>>         }
>>         function getJSON() {
>>                 return $this->json_for_layout;
>>         }}
>>
>> ?>
>>
>> What this component does is allows me to pass in arrays to a global
>> array in the controller, json_for_layout.  Every time I call this from
>> a controller, it add's to the array.  The next step is in my
>> beforeRender() function in my AppController, I do this:
>>
>> $this->set('json_for_layout', $this->Json->getJSON());
>>
>> This sets my view variable.  I then need to actually generate the JSON
>> and output it to a var:
>>
>> <?php
>> class JsonHelper extends JavascriptHelper {
>>         function renderJSON($data, $varname = 'Pastemonkey'){
>>                 $json = json_encode($data);
>>                 $output = $this->codeBlock('var ' . $varname . ' = ' .
>> $json . ';');
>>                 return $output;
>>         }}
>>
>> ?>
>>
>> Now, what I do is inside my header, I echo this out to the view:
>>
>> <?php e($json->renderJSON($json_for_layout));?>
>>
>> And thats it!  Check your DOM tab in firebug to see the global object
>> now available.
>>
>> For example, if I pass in my sitename, I can simply call it with:
>>
>> Pastemonkey.sitename
>>
>> Hope this helps out a few people, and I intend to write a more
>> thorough blog about it tomorrow.
>>
>> On 01/11/2007, Tane Piper <[EMAIL PROTECTED]> wrote:
>>
>>
>>
>> > Apologies for the cross posting here, but I myself am at a bit of a
>> > crossroads in my applications development, and I'd like to take the
>> > time to put out this email in the hopes that it generates some good
>> > discussion, and can maybe teach me a few things.
>>
>> > A little background first to my issues.  I am developing my
>> > application at the moment (pastemonkey.org) and it's being built on
>> > CakePHP and jQuery.  At the moment, I'm using a lot of CakePHP's built
>> > in stuff to generate my HTML, and I'm using jQuery to do some simple
>> > Ajax stuff and some DOM manipulation.
>>
>> > Where I am at a crossroads is how to do my client/server interaction
>> > with JSON.  For example, I have the facility in place for inline
>> > comments.  When you see a paste, you can double click on each line to
>> > create and view the comments.  When you create one, there is code that
>> > generates the comment and enters the line number and paste ID.
>>
>> > To display this, what I do is create an array of the line numbers, and
>> > generate JSON using PHP's inbuilt functions.  So an array like this
>> > ('1', '2', '10', '15') becomes ["1", "2", "10", "15"].  What I then do
>> > is pass this to the view, and do this:
>>
>> > <?php e($javascript->codeBlock('var lines = eval(' . $comment_lines .
>> ')'));?>
>>
>> > So what this does is it outputs
>>
>> > var lines = eval( ["1", "2", "10", "15"]);
>>
>> > I then have this function in my core javascript file:
>>
>> > $('.geshi-output-view ol li').each(function(i){
>> >     i++;
>> >     $(this).attr('id', i);
>> >     if ($.inArray(i, lines) != -1) {
>> >         $(this).attr('style','').addClass('hasComment');
>> >     }
>> > });
>>
>> > What I am doing here is expecting there to already be a variable set
>> > called lines, and I check each line's ID (based on offset + 1) to see
>> > if it's in the JSON array, and if it does apply the class.
>>
>> > It presents two issues - first it's ugly.  It means I always have to
>> > have the javascript helper line above in any view I want to use it in,
>> > and I also expect the variable to be there (in the case of the view it
>> > always should be).  I'm wondering if anyone know a much nicer, more
>> > global way to handle this?
>>
>> > For example, Is there any way in PHP to generate a global object?  For
>> > example a Pastemonkey object that I can attach to.  e.g:
>>
>> > Pastemonkey.Settings.sitename
>> > Pastemonkey.Comments[0]
>> > Pastemonkey.User.Colour.Header
>> > etc
>>
>> > I'm basically looking for the cleanest way to handle persistent data
>> > from my server side that I can use client side with jQuery.
>>
>> > Thanks
>>
>> > --
>> > Tane Piper
>> > Blog -http://digitalspaghetti.me.uk
>> > AJAX Pastebin -http://pastemonkey.org
>>
>> > This email is: [ ] blogable [ x ] ask first [ ] private
>>
>> --
>> Tane Piper
>> Blog -http://digitalspaghetti.me.uk
>> AJAX Pastebin -http://pastemonkey.org
>>
>> This email is: [ ] blogable [ x ] ask first [ ] private
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/How-do-you-do-Client-Server-interaction--%28CakePHP-JSON-jQuery%29-tf4734428s27240.html#a13557354
Sent from the jQuery General Discussion mailing list archive at Nabble.com.

Reply via email to