My-Bic = My Beagle is Cute!

My-Bic Features

JSON client side coding

As of version 0.7 you're now able to send json encoded data back to your server. Thanks to JSON.org for a great JSON encoder!

Working script JSON ClientSide demo

Example:

  1.  
  2. var ajaxObj = new XMLHTTP("mybic_server.php");
  3. var student = new Object();
  4. student.name = "tom";
  5. student.phone = "123-333-4493";
  6.  
  7. ajaxObj.call("action=saveStudent&student="+JSON.stringify(student), callBack);

We created a student object and then used the JSON.stringify function to turn that object into a JSON encoded string to pass to our PHP server script. How do we decode that in PHP?

  1.  
  2. function return_response()
  3. {
  4. require_once(SERVER_ROOT.'mybic_json.php');
  5. $JSON = new Services_JSON();
  6. $student = $JSON->decode($this->queryVars['student']);
  7. $resp = "Student Name: received!";
  8. return $resp;
  9. }

This opens up pretty powerful features for you if you need to develop frameworks and are allowing 3rd parties to access functions and methods on the server side. Do you want them to create a nice little object of parameters to the server or have them make a big ugly querystring?




EASY FORMS!!!

Submitting forms with ajax used to be a pain, you had to create your own query strings and send it to the server... no longer with My-Bic. Watch how easy sending a form with 20, 30, 40, 50 or more fields is:

  1.  
  2. var formVars = ajaxObj.getForm('yourFormID');
  3. ajaxObj.call('action=postComment'+formVars);

That is it my friend! You just create your form with an ID and pass it on in and My-Bic will do the rest. It supports text, textarea, checkboxes, radios, single select, multiselect, arrays, and passwords! To PHP it will look the same as any other form variable submitted to your website. Check out the demo Forms Test




NETWORK DOWN ISSUES!

Working Demo

As of version 0.6 MyBic can help detect Network Down issues for your ajax applications. Lets use an example... You have a webpage that polls every 2 seconds to the server to see if new data is available. If you ajax request cannot get a response back from the server then every 2 seconds your user will be presented with ugly error messages or not even know the requests are failing. MyBic now will test for long server times and actually abort the request after the set time. There are 3 main parameters we care about for dealing with network down issues:

  1.  
  2. // from mybic.js
  3.  
  4. this.net_down_func = this.down;
  5. this.abort_timeout = 5000;
  6. this.failed_threshold = 3;

By default MyBic will alert the user with a hidden div popover indicating a failure as occurred that looks like this:

A network issue has occurred which canceled your last request


You may not want this default method of alerting the user and may wish to use your own function when network issues occur if so just set

  1.  
  2. ajaxObj.net_down_func = yourFuncName;
  3.  
  4. function yourFuncName(status)
  5. {
  6. alert('error occurred');
  7. }

This will allow you to deal with network down issues in your own way if you choose. You could for example write an error message to a particular place on your webpage, do nothing at all or just a simple alert.
Lets take a look at abort_timeout and failed_threshold. By default MyBic will wait 5 seconds(5000 ms) for a request to complete. If it takes more than 5 seconds the net_down_func function will be called and the request will be aborted. You can raise this limit if you know your scripts will run for a long time or you can disable it completely and mybic will wait forever.

  1.  
  2. // wait 10 seconds before aborting
  3. ajaxObj.abort_timeout = 10000;
  4.  
  5. // disable timeouts
  6. ajaxObj.abort_timeout = -1;
  7.  

Also by default MyBic will only try 3 times to make requests. If 3 requests in a row fail then MyBic is disabled. You can override the number of failures by setting:

  1.  
  2. // I only want 2 requests to disable mybic
  3. ajaxObj.failed_threshold = 2;