ajaxboy / cjax

Lightweight Ajax Framework built in PHP with no foot-print. Allows you to build ajax functionality with a single line of code & do so much more, right from the back-end!
http://cjax.sourceforge.net/
66 stars 27 forks source link

Exec function #14

Open jpbalagolan opened 10 years ago

jpbalagolan commented 10 years ago

I have a website with a sidemenu with a cjax click (Exec) attached that loads a controller (CodeIgniter) which loads a view on the content part. This content has a button with another click(exec) event attached to it like the first one but when i click it, it does not fire the event. In your cjax.class Exec function, you have this comment below: //some functions return the ajax object? this is the same part where the issue occurs.

In my point of view, it seems the the later Exec function is not added the the _EventCache according to firebug.

ajaxboy commented 10 years ago

Hi Jp,

It appears you are using Exec with an array, that is the only data that will pass through that statement check " && is_array($actions))".

You are correct, this is not being added to cache, and that is what it's suppose to happen in this case. Since you are using an array within Exec, the actions will be tributed to first element that is used in the Exec first parameter, this means when you click the first element it triggers both actions at once!.

Example: http://cjax.sourceforge.net/examples/bind_actions.php

When you do use an array of action against one element, it will collect all these actions in a single command (that is why you see "unset(CoreEvents::$cache[$v]);" right next to the line you put in question, because it removes that action from the cache, but at that time the action has already amounted/stacked to the first element, as a 'callback' property to the 'AddEventTo' function which is binded to the first element specified in Exec.

$ajax->click('element_id', array( $ajax->alert('Action 1'), $ajax->alert('Action 2'), )); This is what your code is doing ^^^ (sort of speak)

What you need is two click() functions (click() is just an alias for Exec) $ajax->click('element_id', $ajax->alert('Action 1')); $ajax->click('element_id2', $ajax->alert('Action 2'));

Let me know if you have any more issues or questions

ajaxboy commented 10 years ago

In addition, the comment that you see there, that says '//some functions return the ajax object?', that is in relation to really old versions of Cjax, at some point the $ajax chaining object was returned, but this was practice was stopped, now we return exclusively the 'xmlItem' Object

jpbalagolan commented 10 years ago

i dont understand really, my scenario is a partial page loading thing. clicking the sidebar to partially load the content in the

part.

ajaxboy commented 10 years ago

Could you provide your cjax code so I can further help you?

jpbalagolan commented 10 years ago

"$ajax->Exec( 'anchor-element-id' , $ajax->Call( 'path/to/controller' , 'div-element-to-replace' ) );"

i added both of them in the main page.php and in the content.php which will be displayed in 'div-element-to-replace' element.

by the way, i had reverted back to jquery for this to work, but your framework is good.

jpbalagolan commented 10 years ago

i also tried:

  • initializing and calling cjax on the content.php
  • pass $ajax object to controller then to view function in controller
  • put cjax on content.php to main page.php

but nothing worked so far. hope this helps to improve cjax.

ajaxboy commented 10 years ago

Okay, could you post the code you have for both links? I see just one above

jpbalagolan commented 10 years ago

here it is.

-> for mainpage.php $ajax->Exec( 'anchor-element-id' , $ajax->Call( 'path/to/controller' , 'div-element-to-replace' ) );

-> for content.php "$ajax->Exec( 'anchor-element-id' , $ajax->success("Success!") );"

at some point tried in content.php

$ajax->success("Success!");

it works but not the with the Exec function. the success function should have been a custom dialog form not the success dialog. just use it for testing exec function.

ajaxboy commented 10 years ago

There is your issue right there... an element id is unique on the entire page (this is true for any page on the web), having the same id in more than one element will create conflicts. If you just rename one of your elements something else, then it should start working the way it is supposed to.

I guess using the same element id.. has caused cjax to think you are using an array in that element.. hence you ran into the issue that you mentioned above. Notice how in the example I gave you, I included element_id and element_id2.