0

Please forgive me if this has ever been asked before, it's very hard to search for since the operative word is 'this'.

I have a click event bound to a set of divs with the same class, each one needs the same behavior. In jQuery, I would simply use $(this) to reference the clicked element. I can't seem to figure out how to get CakePHP to use $(this) as the update element.

My code: (the 'update' param is the one in question)

$this->Js->get('.bookmark')->event('click',
    $this->Js->request(
        array('controller' => 'bookmarks', 'action' => 'add', $event['Event']['id']),
        array('async' => true, 'update' => 'this')
    )
);

Thank you

1
  • Forget JsHelper and write straight up jquery code. It's pretty limited in functionality and will end up slowing you down instead of speeding up your development. It's been removed in CakePHP 3.0. Commented Aug 31, 2013 at 12:18

3 Answers 3

4

My personal opinion - don't use the CakePHP JsHelper. Don't get me wrong, I love CakePHP and am a CakePHP fanboy, but - in some cases it's just easier to use the "normal" way.

As an example, the HTML helper can create divs too, but I've never found the reason to use it over just typing <div>.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm beginning to come to that conclusion myself. I like to try doing everything the 'cake way', but I think it might be easier, more readable and more maintanable to just write javascript as javascript. (though I will admint to having used the div function...)
Yeah - even us cake lovers don't use ALL the helpers. :)
0

I've used this before for select lists (I modified it for your example, so I'm not 100% sure it works), I don't see why it wouldn't work for click

Put this at the bottom of the view

$this->Js->get('#addBookmarkDiv')->event('click',  //put the div name that contains the input you're checking to see if it's clicked
    $this->Js->request(array(
        'controller'=>'bookmarks',
        'action'=>'add'
        ), array(
        'update'=>'#putdivhere', //div you want to update, possibly a div that displays bookmarks?
        'async' => true,
        'method' => 'post',
        'dataExpression'=>true,
        'data'=> $this->Js->serializeForm(array(
            'isForm' => false,
            'inline' => true
            ))
        ))
);

Now, in your controller do what you have to do, and add the following

 function add(){
    //do what ever it is you want to do with the sent data
    $this->set('bookmarks',$bookmarks);
    $this->layout = 'ajax';
 }

Then, create a view for add with something like.

//add.ctp

<?php foreach ($bookmarks as $bookmark): ?>
<li><?php echo $bookmark; ?></li
<?php endforeach; ?>

1 Comment

The problem I'm trying to solve involves the case where you would bind the click event to a class of divs, so many elements have the same behavior. In that case, the only way to know which element to update is to access the this var since it could be any of a number of clickable elements. It seems that all documentation on the JsHelper assumes I only have one element which is clickable and only one element which has the potential to need updating.
0

Well, it looks like I was overlooking that fact that I could embed javascript in the JsHelper functions. That and the fact that $(this) is out of scope inside the $.ajax function. So, this is the solution that worked for me. As you can see, the second parameter to the event method is a mixture of javascript and PHP, and it's strung together in the first instance using concatenation.

$this->Js->get('.bookmark')->event('click',
    'var el = $(this);'.
    $this->Js->request(
        array('controller' => 'bookmarks', 'action' => 'add', $event['Event']['id']),
        array('async' => true, 'complete' => '$(el).addClass("bookmarked")')
    )
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.