mercredi 2 octobre 2013

BlackBerry10 and Ajax

In the world of BlackBerry 10 was the choice of developing our applications in pure web (HTML5, javascript, CSS) with WebWorks, or native (c ++ and QML) with the framework of the waterfalls.


At WebWorks, the proposed webview is bright: contains all the features of HTML5 that you want the video to the list of contacts through the localStorage or different sensors of the terminal.


On the other hand, the falls side, if one had the idea that the hybrid and that, for example, native navigation and viewing the web with a webview, a content of bitter surprise awaits us: the webview is minimalist, not all API in WebWorks and same miserably buggy in some points.


One of these points is AJAX, but there is a solution, here's how to get there.


AJAX, feature that allows to dynamically load data in a server, JavaScript, without leaving the current page, security problems: theoretically, nothing prevents a benevolent javascript code to insert a malicious script which was published in an unknown server all of your personal data. At the time, most of the time, in a classic site, script must come from the server that serves the web page or a known CDN (Google, Yahoo, etc.).


However, on the side of mobile web pages can be embedded in the application (locally in the terminal) and call remote scripts JavaScript. An operating system not suitably or block all remote sites (also compulsory, could already do not develop HTML5 apps), or allow all of them (dangerous).


This is why iOS or Android, if using Apache Cordova, found a 'white list': each application defines a list of the servers authorized to provide JavaScript code. This level of security has been added by Cordoba.


Side WebWorks, an XML file to configure the whitelist of the areas authorized for AJAX. Falls on the side of the hand, nothing else is planned: all AJAX calls are blocked and return error 0: signal were blocked for security reasons.


Why BlackBerry has not helped the webview of waterfalls has the same wealth as WebWorks webview? They were in a hurry? Do not proposed that some would try to combine web and native?


One of the advantages of waterfalls is their language, the QML: a simple mix of Javascript and language key/value. If your code in Javascript has already done their weapons in frameworks such as Sencha Touch or Enyo.js, you know QML-encode and you can make an application of BlackBerry10!


And the good news is that in Javascript, QML, Ajax side are no blocked calls! And that the webview and native relatively easily can communicate with each other. Neither one nor two, so here we are embedded in a palliative for the former: a bridge between the web and native to communicate with the outside world through Ajax.

Web, overwrites the $ function.AJAX method used by jQuery or our Zepto.This sends all the settings and options of the AJAX call to the natives.Web stores in the passage of a function that will process the result and sends the ID of this function to the natives.The native of made the call Ajax with maximum options (POST or GET, type configurations, etc.) times jQueryUne $.ajax the result obtained, the native network sends the result to the function ID previously received.Web calls the function identified by passing the results received from the ajax request.Web calls the callback "success" or "failed" originally passed to $.ajax to process the result.

Therefore need to change their habits, don't call your function $.ajax, $.post or $.agarra as before!


You will find an example of this code in Gist here. This 'fix' works to jQuery and Zepto but surely adapt to other frameworks.


download_gist


Although we don't use it as what this snippet (have packaged a little better than that, a day that will be shown) the key is there. We use this technique successfully in various BlackBerry applications is developed.


We agree that is a very good solution, but in this case, BlackBerry can provide for better the needs of its developers.

Aucun commentaire:

Enregistrer un commentaire