![]() |
Xataface
2.0alpha2
Xataface Application Framework
|
As of Xataface 2.0, there is a much greater emphasis on Web 2.0 and Javascript. The Dataface_JavascriptTool now allows you to build complex javascript applications with dependencies and have them compiled (and cached) at run-time. You can develop your own Javascript libraries and include them with your module easily. This tutorial describes how to bundle your javascripts, and how to make them available to your actions (and other actions too if you so desire).
The Dataface_JavascriptTool class provides all of the functionality necessary to build javascript applications. We are going to make use of 3 main methods of this class for this tutorial:
js directory as a location where Javascript files can be found.In this tutorial we are going to use Javascript to turn our "Hello World" action into a more interactive action where the user can type in their own name and have the text changed to "Hello <Their Name>" depending on what they enter into the text field.
First let's modify our template to prepare for interactivity. We need to do two things:
span tag to the heading so that we can reference and change the "Hello World" text more easily.The resulting template now looks like:
Notice that we referenced the span, input, and button tags with id attributes so that they are easy to reference from Javascript.
We're going to store all of our javascript files inside a directory named js inside our module's directory. I.e.
Further, we're going to follow the best practice of creating a sort of namespace for our scripts (just like we did with our templates from the previous section) so that we don't conflict with the script locations in other modules or Xataface itself. Hence underneath our js directory we're going to create a directory: xataface/modules/hello_world and place our javascript file inside that. I.e. the full path within our module's directory to our javascript file will be:
Inside the hello_world.js file add the following:
Now there are a few things to notice about this Javascript segment:
jquery.packed.js . This declaration is handled by the Dataface_JavascriptTool at runtime to ensure that the jquery.packed.js file has been loaded. This ensures that jQuery is available to use in this file. The jquery.packed.js is located in the xataface/js directory which is automatically and always included in the Javascript Tool's list of javascript paths so that you can always reference files from it.require statement) is wrapped in a function structure. This is a javascript technique for makeing namespaces so that variables that we create inside this file do not pollute the global namespace. See This thread for more information about this technique. It is best practice to always use this technique in your own javascript files to make the source base more manageable.click() event on the update-user-name-btn button and then change the "world" text to whatever text is entered into the text field.Now that you have created your javascript file, let's modify the hello_world action to use this javascript file.
The only change that we made was to register our js directory with the Javascript Tool so that it knows to look there for Javascript files. Next we just import our hello_world.js file so that it will be executed on page load.
At this point we're ready to try out our action. Just point the browser to your hello_world action again (e.g. index.php?-action=hello_world
Shifting from a PHP-centric application to a javascript-centric application can present a bit of a learning curve while you get used to the new environment. Sometimes debugging Javascript can get a little tricky. The trick is to use the right tools and know where to look. If you are debugging your application or developing it, the first thing you should do is turn on debugging in the Dataface_Javscript tool. You can do this by adding the following section to your application's conf.ini file:
This will prevent the Javascript Tool from minimizing the code. It will also turn off the cache. Once you have finished debugging you should remove this directive again (or comment it out) as it will cause performance to be much slower.
Once you have enabled debugging, the next step is to use your preferred Javascript debugging tool to debug your application. I generally just use Safari with the Develop menu enabled, but most of the modern browsers now include some mechanism to debug javascript.
1.8.1.2