Topic: xajax to jquery

It seems that xajax is really not making any more progress, so its finally time for me to think about dumping xajax. Now, in the process for a solution i noticed a interesting piece of code on the net yesterday.

http://stackoverflow.com/questions/1615 … e-function

Using this code, and some changes here and there, i created the following.

doajax.js

$.showMessage = function(func,time) {
  div = $('<div id=debug>').html( func );
  $("body").prepend(div);
}

$.doAjax = function(values) {
 $.ajax({ data: values, url: '/ajax.php', type: 'POST', dataType: 'json', timeout: 1000,
  error: function(xhr, ajaxOptions,thrownError){ $.showMessage('Error processing request',10000);$('#debug').html(xhr.responseText); },
  success: function(data, textStatus, jqXHR){
        $.each(data, function(){
          if ( this.action == 'attr' )        $(this.target).attr(this.content);
          if ( this.action == 'removeAttr' )  $(this.target).removeAttr(this.content);
          if ( this.action == 'addClass' )    $(this.target).addClass(this.content);
          if ( this.action == 'removeClass' ) $(this.target).removeClass(this.content);
          if ( this.action == 'html' )        $(this.target).html(this.content);
          if ( this.action == 'text' )        $(this.target).text(this.content);
          if ( this.action == 'val' )         $(this.target).val(this.content);
          if ( this.action == 'eval' ) {
            eval(this.content);
          } else {
            $(this.target)[this.action](this.content);
          }
        });
      }
 });
 return this;
};

ajax.php

<?php

  $base = $_POST['base'];

  include_once("your/ajax/code/path/" . $base . '.php');

  $objResponse = $base();

 // header("Content-type: text/plain");

  echo json_encode($objResponse);

?>

This php file, will act as central call platform for your ajax functions. This allows you to things like setting up the database, clean up your variable input, and other things your basic ajax function / file needs.

In your xajax Modules change :

xModule

<?php
  function xModule( $module_id )
  {
     // Do stuff

    $objResponse = new xajaxResponse();
    $objResponse->assign( "your_id", 'innerHTML', $html );
    return $objResponse;
  }
?>

To:

xModule

<?php
  function xModule()
  {
    $module_id = $_POST['module_id'];

     // Do stuff

    $response = array();
    $response[] = array( 'action' => 'html', 'target' => "#your_id", 'content' => $html );
    return $response;
  }
?>

Call the function. In my example, you always need to provide the "base" module name in your string.

<script type=\"text/javascript\">
   $.doAjax( 'base=xModule&module_id=123' );
</script>

I hope this helps people who want to make the switch away from xajax. By doing this, you are able to have both the basic functionality of xajax, and the extended abilities of jQuery. And saves you on js code that your site needs to download.

* Did i mention that this is actually faster then xajax.
* Also, no need to register your xajax functions in advance anymore.

I'm not saying this is a perfect solution, and it does not cover some of the advanced features that xajax has, but for people who do the "basis" things, its a starting point. The doAjax needs some more work, like better error handling, debug information, but it is usable as a example.

Re: xajax to jquery

Hi,

Interesting approach.

I guess I don't know what people want from xajax.

While development is at a standstill, the framework is fully functional, easy to use and very fast.

What additional functionallity do people see as necessary?

Did I mention that jQuery works well along side xajax?

Did I mention that you only need to register a single function when using php 5.x?

    /**    
     * Standard xajax function dispatcher 
     * The only function registered with xajax...    
     * @param string $function_name - called function
     * @param mixed  $data - browser form data
     *
     * All templates call xajax_dispatch('func',data);
     * Uses php 5's function variables feature.
     * Validate that the function exists and return it
     * passing in the data. Else alert the error.
     */ 
    function dispatch($function_name, $data='')
    {
      $this->data = $data;
      if (method_exists($this,$function_name))
        return $this->$function_name($data);
      else
      {
        return $this->xmsg("Invalid function name passed: $function_name");
      }
    }

This particular code is in my Codeigniter controller class...



Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: xajax to jquery

edrobinson wrote:

I guess I don't know what people want from xajax.

What do most people want? The power of Ajax, without the hassle of coding JS all the time.

edrobinson wrote:

While development is at a standstill, the framework is fully functional, easy to use and very fast.

A framework that is still in beta1 status, with bugs reported ( possibly fixed but not released in a new "version" ), documentation that refers to old functions, support that is limited to people visiting the board every few days, .

edrobinson wrote:

What additional functionallity do people see as necessary?

The ability to use more features that are present in jQuery, or other frameworks, without including too much JS in your end result. See below.

edrobinson wrote:

Did I mention that jQuery works well along side xajax?

So you want people to use a 90k package ( jQuery ) + another 100k package ( xajax ) + Generate JS, when one can run 90k ( jQuery ) + a 1KB script, that does the basic's that xajax does.

It complicates debugging as your running two packages. The extra download size for the end client on first visit.

Do you not think its silly, to run two different packages, when one can do the basic functionality?

How about jQuery being constantly updated. Good documentations. Bugs fixed at regular interval.

There was a annoying bug in the i think, B1 version, that i ended up fixing on my side of the xajax code. It has been reported here, but think it was never fixed. Been to long ago to remember correctly what the bug was.

edrobinson wrote:

Did I mention that you only need to register a single function when using php 5.x?

This particular code is in my Codeigniter controller class...

I assume your include your functions before your running your xajax calls. Only problem is, what happens when your end up with more then a few files / functions calls ( there is a reason why people requested the register ability years ago ).

Now, i will admit, it has a been a while when i last looked up xajax's documentation / features. One of the reasons is because the documentation has been horribly out of date. Check the docs, and you will find examples that go back to the older versions, who are just not valid anymore.

Look around edrobinson. A few years xajax had some traction, but over time, it simple started to fade away because of the reasons i mentioned above. And this is from somebody who used it over a LOT of years. This nick is new because i forgot my old accounts password. Been here plenty of years. How long did it take again to move from XML to JSON for data transfer?

In some not really representative tests, my conclusion is, that this approach is actually faster then xajax. Another gain.

To me, this approach, that somebody else figured out, saves me the trouble of including a whole load of JS in my websites.

In my option, xajax is not worth the trouble anymore for anybody new to use. It also does not help, when the main website is STILL reporting the stolen domain name from so long ago, as the "latest" news. Anybody looking at this project sees a project that looks more dead then anything else.

And a dead looking project = security risk, because if there are know exploits or bugs, that do not get fixed.

You are about the only "official" person that is left keeping this board somewhat alive.

Anyway, got to go, need to pick up my Wife.

Re: xajax to jquery

OK.

1. Xajax was not written to be a jQuery like library. It is a library to handle browser <-->server traffic with a minimum of hassle. I do no JS coding to use xajax. You complain about using JS but, using jQuery, that's all you use client side.

2. What bugs are outstanding in the beta? I have been using it in production since shortly after its release with no issues whatsoever.

3. If you want jQuery features then USE jQuery!

4. The latest jQuery compresses is 93K and my xajax.js compressed is 42K neither of which take an excessive amount of time to download.  How many other jQuery plugins or things like the jQ UI library do you use? I have no problems debugging.

5. I would guess that anyone would have included their functions before trying to call tham...

XML to JSON - so what? I have not seen any outward difference because of it. Could argue about the benefits of either for days.

"And a dead looking project = security risk" - "The sky is falling! The sky is falling!"

I once had a client that used an AS400 as his core business system. IBM wanted to sell him an expensive OS upgrade and he asked my opinion. I asked him if the current OS was serving his needs and would he ever want to add an application that might rely on later versions. He thought about it and passed on the upgrade. The system was still alive and well a few years ago when my client passed away.

Please let me know if remember your old nick and I will look your posts.

A few of us users have talked about getting together to continue development but so far nothing has come of it.

BTW I mean it when I say "Interesting approach."

Forgive any percieved sarcasm. smile

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: xajax to jquery

To be honest edrobinson, seeing your responds made me think this discussion is just a wast of both of our time. I thank you for your time, but this will be me last time posting here. I hope this script is useful for other people. Lets leave it at that.

Re: xajax to jquery

Sorry you feel that way...

I have been working with your suggestions all afternoon and I feel it has some good prospects.

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

7 (edited by benjiro 2012-04-07 12:47:58 AM)

Re: xajax to jquery

Seeing as q_no showed up, let me be a pain in the behind again big_smile

I'm not posting this in q_no's thread as i do not want it to go offtopic.

$objReponse->jQuery('#mydiv')->html('New content<br />for my div')->fadeIn();

xajax does html, set attributes, classes, etc, and jquery does the same ( and more ). In other words, you going to wrap a class system around jQuery, and integrate this into xajax ( as a extension ). Maybe its me, but it sound like you just end up duplicating the same set of abilities, in the same project ( even lets assume that the jQuey wrapper is a add on ).

In fact, what is the point of the xajax core, when one can just end up using a jQuery's wrapper class from the start?

but one of the main purposes of xajax is to reduce amount of hand-written JavaScript to a minimum. smile

Funny thing about that. That little prototype class where edrobinson & i where not agreeing upon does exactly that.

I have been converting my xajax scripts to this little $.doAjax system, and i ended up with the same abilities, technically ( beyond the base class ), there was almost no js involved, and i have a dislike for exposing too much functionality to the user side ( especially JS ). At this moment 80% of my script have been converted, and they actually ended up being more simple. Yea, i was also surprised about that. The only 20% left to do, is that table extension.

How much JS / jQuery code did i need. 1 JS/jQuery Script to replace the ModelWindow extension ( easy to find as there are dozen / hundreds ). Some quick tweaking. And voila. It actually ended up being graphically more "pleasing" thanks to the jQuery effects, and simpler to implement. Maybe my xajax implementation s*cked?

Here is a updated version of the code, with some improvements ( like jQuery 1.8 ready, and some tweaks ).

/* doAjax allowing DOM manipulation based upon actions provided from json array. 
 * jQuery 1.8 ready.
 * 
 * Call ( values ) expected the following parameters ( structure as base=?&function=?&... )
 * TODO: Split out the base/function. $.doAjax = function(base, function, values)
 * 
 * @param base: The base file
 * @param function: The base function
 * 
 * Callback ( data ) expects a json encoded array, with the following:
 * @param action: The action to be performed by jQuery: Example: attr, removeAttr, addClass, removeClass, html, text, val, eval, ...
 * @param target: The # ID target to perform the action on
 * @param content: The content ( if needed ) to do this action on.
 */

$.doAjax = function(values) {
  $.ajax({
    data : values,
    url : '/ajax.php',
    type : 'POST',
    dataType : 'json',
    timeout : 1000
  }).fail(function(jqXHR) {
    $.showMessage('Error processing request', 10000);
    $('#debug').html(jqXHR.responseText);
  }).done(function(data) {
    $.each(data, function() {
      try {
        if(this.action == 'eval') {
          eval(this.content);
        } else {
          $(this.target)[this.action](this.content);
        }
      } catch(err) {
        $.showMessage(err.message, 5000);
      };
    });
  });
  return this;
};

Does this not exactly do what  q_no is planning to do? With the only difference, that the json return array is not in a nice fancy wrapper.

If i where to hand this to somebody with no experience in both xajax & this code, i'm willing to bet, that they will be more easy getting going with this, then with xajax. Really, xajax gets sometimes so complicated for no good reason.

This will give you a idea how xajax ended up on one of my older projects

// ====================================================
//  Xajax
// ====================================================

  require ( EXTERNAL_DIR . "xajax/xajax_core/xajaxAIO.inc.php" );

  $xajax = new xajax( SERVER_URI . '/preload.php' );

  $xajax->setFlag('debug', ( DEBUG_ACTIVE == 1 ? true : false ) );
  $xajax->configure('scriptLoadTimeout', 0); // get rid of the second (timeout) script by setting
  $xajax->configure('deferScriptGeneration', true); // Hide the JS code from xAjax
  $xajax->configure('javascript URI', 'Framework/External/xajax/');
  $xajax->configure('inlineScript', 'false'); // Set tableUpdater etc to use a js file, instead off inline

  require_once( EXTERNAL_DIR . 'xajax/xajax_plugins/response/modalWindow/modalWindow.inc.php' );
  require_once( EXTERNAL_DIR . 'xajax/xajax_plugins/response/tableUpdater.inc.php' );

  $functions = array(
   // 'xFieldEditor'          => array( 'xFieldEditor', 'xFieldEditorClose' ),
    //'xReportTemplateList'   => array( 'xActorTemplateList', 'xActivityTemplateList' ),
    //'xReportTemplateDelete' => array( 'xActorTemplateDelete', 'xActivityTemplateDelete' ),
    'xActions'              => array( 'xActions', 'xActionsAppendRow', 'xActionsAddLine', 'xActionsDeleteRow', 'xActionsAddChild' ),
    //'xRoles'                => array( 'xRoles' ),
    'xActivities'           => array( 'xActivities', 'xActivitiesAppendRow', 'xActivitiesAddLine', 'xActivitiesDeleteRow', 'xActivitiesAddChild' ),
   // 'xContent'              => array( 'xContent', 'xContentAppendRow', 'xContentDeleteRow', 'xContentProperties' ),
  );

  foreach ( $functions as $file => $methods )
    foreach ( $methods as $method )
      $xajax->register(XAJAX_FUNCTION, new xajaxUserFunction($method, CORE_DIR.'Xajax/'.$file.'.php'));
  $xajax->processRequest();

+ The JS initialization in the head of the html.

This entire cr*p has been replaced with 3 lines of JS include in the head  ( jQuery, doAjax, doModelWindow ). With only missing a replacement for the tabletUpdater. And do NOT get me started on the amount of pain in the behind that has been. Lost a LOT of hours debugging, and finding out why xajax refused to start correctly with some other settings. Too long to get into.

It took a while for me to get my head around jQuery, and i NEVER really liked how you ended up with too much JS in your HTML code to deal with this. And pushing data to that JS. But with this stupid little doAjax class, i actually LOVE jQuery. Maybe my way of using is not how it was design to be, but i do not care.

You wonder about something: Google + jQuery + Question, and in most cases you have a good simple answer how to get a effect you need. I can not say the same about xajax. For xajax you ended up combining it anyway with other scriptalicious or other packages, if you wanted any of the more "fancy" effects.

Looking at web support for xajax, mostly you find things from 4 or 5 years ago. With function that are not used anymore in the 0.5/0.6 functionality. Dead example pages. Find me some other places that give xajax support beyond this board, about neat tricks or easy solutions?

If there is one thing i noticed in the past, when i was looking for a other solution because of xajax its slow ( or dead ) like response, that very few people ever figured out how to make jQuery work like xAjax. That there are actually people who like to control the jQuery, Ajax, from there secure PHP server, without exposing to much functionality.

Anyway... This is mostly a responds to q_no. Welcome back q_no, but i fear i am one of the fallen Sheep smile Funny thing is, take this script, change the name from $.doAjax to $.xAjax, add a more nice wrapper, and you have xAjax and beyond. Hell, will not surprise me, that some of the original xajax functions name can be take over in the wrapper, to make it a xajax clone like ( but with more future functionality ).

Re: xajax to jquery

Hehe

Hello Benjiro,

my example of using jQuery was only an idea to start a discussion. I'm glad you brought this up and pointed out another way of doings things.
Your JavaScript response handler is very basic and only covers a very small amount of what Xajax is capable of, but I see where this is going.

To be honest, while tossing in the "Xajax Jquery Plugin idea" I was also thinking about writing just a small Xajax wrapper to hook into JQuery, instead of going the other way around. I do like this idea, but the way the Xajax JS core works is not gonna change for the long overdue final of 0.6.

I am however open for creative suggestions, even if they are radical, for Xajax 0.7(?) and beyond.

I've seen discussions like this one years ago, when CtC took over the development of Jared's 0.5 beta1 release. BigBrownChunxx (who was maintaining the 0.2x. branch at that time) wanted to keep xajax tiny & neat, while the development of 0.5 was (partly) a bit over the top... For instance... CtC implemented a pretty nice set of Xajax control classes that can generate the HTML&JS code for entire applications. Although I do like what CtC has created there from a technical point of view, I was also skeptical at the same time whether that's what users want from Xajax or not.
Obviously not. The control classes have been removed in 0.6.

From my point of view, Xajax has to loose more weight, without compromising the rich feature set too much. Your JavaScript response handler for instance doesn't cover any kind of error reporting, no hooks for 3rd party tools, no charsets other than UTF8 and the list goes on.

I'd love to see a fork that sits tight on top of Jquery or mootools or whatever, but not for the cost of getting the rid of most functions. That's why I came up with the compiler idea. Some people only need a hand full of basic response commands to modify the DOM and a robust transport  - no debugging, no error handling... if the compiler could take care of that and drop all the overhead while giving u the option to upgrade the core any time later, it could work nicely for all of us!
You could take this approach even further and replace the entire transport part with a few lines of jQuery, while still maintaining compatibility with the PHP core.

Your turn! smile

Re: xajax to jquery

q_no, can you give me a example of the function you descriptor where xajax exceeds? Because to be honest, from my experience with xajax, most functionality is most of the time limited a) to basic calls, b) the plugins.

This script example is exactly that, a example. You state the missing error handling, but i never released the complete code. Because my framework's error handling handles that ( and is reported back to the script ). You do not think that i will just get data directly from a $_POST request. *haha* No ... this is also filtered in advance. Same with some other things.

And lets not get me started on error handling with xajax. How long did it take so that xajax output its debug to a div instead of that *biep* popup? That was on hell of a future that was requested plenty of times. And i'm not started on the hundred of times, that xajax shocked on some problem, leaving you in the dark as the error handling was useless. To be very honest, most of the time the error handling was just that, useless. All it took to make you wonder what the hell was wrong, was a single! extra line/character beyond the "?>" end of your script. That alone was able to give you errors that made you lose plenty of time to track it.

You want proper error handling: Firebug, or other debugging for JS related problems, and your own error handling to capture anything PHP trows at you. Guess what gets captured in the example above ( and my own stuff that is not posted above ). And no, i'm not saying its perfect. Hell no. But for me, it beats xajax's error handling!

Third party plugin is nice, but lets see how many official plugins xajax actually has? 3 in 0.6 release, and 4 in the 0.5 release if i remember correctly. And some onofficial one's, with half not working anymore, as ... written for 0.2. The same with plenty of info sites, that first showed up when xajax become popular. Most are gone or incomplete these days.

UTF8 ... Maybe its better to first look if there is support in area's that use a other character set ( like GBK ), before a option like that needs to be implemented? Sure, Xajax can be supported in China? Are there a lot of Chinese users? I did not see a lot of Chinese users on this forum, asking for support. UTF8 is basically half the websites on the internet. I'm not saying that one needs to avoid or disregard a growing part of the internet, but one needs to stay realistic.

xajax has 956,000 results on a quick google search. This for a ajax solution that has been there from the start of the ajax "revolution". With a lot of those being about version 0.2.

Forking xajax, to integrate jquery sound a bit silly idea. Like i stated above, i really do not see a lot of abilities in xajax, that make it stand out beyond a transport / 3Th party plugins.

Lately on a lot of job notices, i say the following under the knowledge section: jQuery. Because a lot of companies use it these days. I have never in my entire life, saw one advertisement for xajax.


To be honest q_no, i am sure, that its very possible to create a wrapper functionality, that allows the "cleaner" syntax that xajax uses, and 3th party plugin support.

Actually, talking about this wrapper thing, i noticed this in the past: http://jquery.hohli.com/ This already does it ( with a different twist then the example posted above ). The only problem, is there is no support linked to it, and again, not maintained ( relying on a very old version of jQuery ).


The compiler idea is something that actually also comes up in a lot of jquery topics. The funny thing is, there the developers clearly say "NO" to this request. Because while it sounds nice at first, it becomes a problem the moment you think about 3th party plugins. What happens when a plugin like the TableUpdater requires a features that is not present in the compiled version.

With the 0.5 release, it got out, without anybody checking the Tableupdater code, to see if it actually worked with the changes done in 0.5. With a end result, that anybody who updated and used the Tableupdater, had unexplained problems with his scripts ( that used the TableUpdater plugin ). *holds up hand* Yea, i found the topic again, 5 month later, somebody else also stated that the bug was not fixed ( i do not remember for how long before it was fixed, if ever in the 0.5 release? ). For the 0.6 release it got fixed. But lets be realistic, a release with 2 bugs in a script, that never got updated for months, even after the fix was posted under the bug section CLEARLY, with a exact line number, and problem. Meaning every person who downloaded that version, and used the plugin / configuration, had this potential problem.


Do you realistically expect people to come back? What if people do come back. Most of the information on the web is 0.2 version, even this websites information last time i checked, was out of date. And incomplete.

These are just my opinions, but for one, i advise for any new version, change the name. So that people do not mistaken information about the older versions, with the new one. For me, xajax is a done deal. I'm just discussing this, to me my motivation know ( maybe people learn from it, maybe they do not ). But when one can replace xajax, with technically 3 script, for 80% of the functionality. Ending with a faster version, lighter ( no xajax needed, no plugins, no other JavaScript Library needed for special effect ), and about 1000's of potential 3th party scripts available that can be integrated.

I'm not saying that xajax needs to pack its bags, but realistically, one does need to think over the future you want for xajax.  Cutting out the weight, ... then what? How about the speed, realistically, it needs a major speedup. Are it those features you mentioned? Who knows, but more importantly is not just releasing some cuts & features, but also maintaining a support. edrobinson has been the only one resembling any "official" support. There are plenty of topic of people asking for help, reporting problems, that gone unanswered. Its not just about maintaining something, but also that people feel that the project has life in it.


Your turn smile
Tjuus.

Re: xajax to jquery

Hello Ed,

i saw this thread and also your example how to reduce the amount of registered functions to one. I really want to reduce the amount of visible javascript within the website header, so i'm trying to setup your example in my project.

As a matter of fact i don't quite understand that code yet because within my project, i do not just register normal functions but instead i register methods of my classes by calling
$xajax->register(XAJAX_FUNCTION, array("myMethod",$this,'myMethod')) within their contructor.

So how would i go about if i'm using dispatch('myMethod',data). How can i route the given funtion to the correct class method after my php receives an ajax request?

Also what do i do with undefined amount of ARGS? Say if myMethod has more than one agurment. Would i call the dispatch like this: xajax_dispatch('myMethod',Arg1.Arg2,Arg3,...)?

I'm kind of confused, hope you understand question.

kind regards
Charset

Re: xajax to jquery

Charset,

I have changed my dispatch function just a littls:

    function dispatch($function_name, $data='')
    {
      $this->data = $data;
      if (method_exists($this,$function_name)&& is_callable(array($this,$function_name)))
      {
        return $this->$function_name($data);
      }
      else
      {
        $resp = new xajaxResponse();
        $resp->alert("Invalid function name passed: $function_name");
        return resp;
      }
    }

Just added is_callable() and used an xajaxResponse in the error return.

You can register dispatch() so:

$xajax->register(XAJAX_FUNCTION, array("myMethod",&$this,'myMethod'))

Example of a button onclick to submit a formwith id="form1":

<input type="button" value="Send Form" onclick="xajax_dispatch('my_form_processor', xajax.grtFormValues('form1')) />

This sends the form data and function name to dispatch which in turn calls my_form_processor using

return $this->$function_name($data);

my_form_processor does something - anything - and returns an xajaxResponse object as do all xajax enabled functions.

Why this works is that my_form_processor was called from $xajax->processRequest() which doesn't care where the returned response object comes from. So any function in the class can return it.

As to passing multiple values, I have never needed to... I pass form values, no values or one value.

You could use json_encode in your client side and json_decode in the php or you could make your values into a string with a common seperator and use explode() in your php.

Hope this helps and not confuse further... smile

Ed

Edit:

To use this function outside of a class - in a non-oop php page requires a bit different function:

    function dispatch($function_name, $data='')
    {
      $funcs = get_defined_functions(); 
      if (array_search($function_name,$funcs['user]) !== FALSE) //Security...
      {
        return $function_name($data);
      }
      else
      {
        $resp = new xajaxResponse();
        $resp->alert("Invalid function name passed: $function_name");
        return resp;
      }
    }
If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.