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

Reply via email to