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

Reply via email to