There are a couple of ways to handle this. The most straightforward
way would be to create a global JS var. You can do this by having Cake
generate a JS block like so:

$javascript->codeBlock('var post_key = ' .
$this->data['Note']['post_key'] . ';');

As long as the jquery code that needs to access post_key is inside
$(function() {}) (ie. "$(document).ready()") it'll be able to access
it.

The better and cleaner way, imo, would be to have jquery get the value
from the element itself. Make the element with ID 'notes' instead have
a class, "Edit" and an ID of 'edit_' .
$this->data['Note']['post_key']. Then change your JS code to:

$(function() {
    $('.Edit').each(function(){
                var id_parts = $(this).attr('id').split('_');
        
                $(this).editable('/notes/mynote/' + id_parts[id_parts.length - 
1] {
                         id        : 'data[Notes][note]',
                         name      : 'data[Notes][note]',
                         type      : 'text',
                         cancel    : 'Cancel',
                         submit    : 'Save',
                         tooltip   : 'Click to edit the title'
                });
        });
})

It's not great, that's just off the top of my head. But it allows you
to have an ID with any number of underscores. Just as long as the
numeric part is last. I guess you could use a RegExp, also.

Unfortunately, with a DIV, there's no other attribute you can save the
php var directly (ie.  just the number) to, and HTML elements can't
have an ID that's only a number. I use this technique with anchors
sometimes, where I can store the PHP var in the anchor's rel attribute
(ie. just the number--no parsing in JS). Very handy.

On Thu, May 7, 2009 at 1:09 PM, Dave Maharaj :: WidePixels.com
<d...@widepixels.com> wrote:
> I finally have the courage to drop the built in Ajax helper and expand into
> jQuery. So my first question so far is how do i pass a variable to jQuery?
> In the function below where VARIABLE HERE is i need to pass
> data[Note]['post_key']
> (data[Note]['post_key']  is just a unique identifier string something like
> fr567 or ht67k)
>
> $(function() {
>     $('#notes').editable('/notes/mynote/ VARIABLE HERE ' {
>          id        : 'data[Notes][note]',
>          name      : 'data[Notes][note]',
>          type      : 'text',
>          cancel    : 'Cancel',
>          submit    : 'Save',
>          tooltip   : 'Click to edit the title'
>     });
> });
>
> Thanks
>
> Dave
> >
>

--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to