Moved

After some time I finally made the move and bought a domain name and hosting. Unfortunately I did not have the time to build my personal web site yet (I want to do a nice job on it). Instead I din another thing: I moved my technical blog from 42answers.wordpress.com here. So if you read anything on it before, or added it into the RSS reader or linked to it (thanks btw) I will have to ask you to update those links. Posts will not continue on 42answers, and in some time the account will be deleted entirely.

And since I made the move to my own domain name and space, and I can put up whatever I want I have big plans:

  • I will post things on this blog more regularly
  • I will also add a more personal blog, in Romanian. I’ll link to it when it is up and running.
  • It will be easier to expose my projects and any related files / materials. So hopefully, it will be easier for you to get what you want.
  • More content. Yeah, I’ve also added this at bullet no.1, but it’s worth repeating.

All of the posts and comments from 42answers have been moved here. Now I still have to move over the blogroll links and tweak some settings.

See you soon in the new home!

Wikipedia out of ca$h?

Acording to a declaration made by Florence Devouard, Wikipedia may be in the slumps – “At this point, Wikipedia has the financial ressources to run its servers for about 3 to 4 months.”. This may very well be just arather extraordinary example of begging for money, or a way to put itself on the shelf, but since it is the most popular way to get information it may be possible.

Wikipedia has risen in a while to a de facto way of gathering information on the web. Either for a project, for personal information or for work, it provides. It’s one project that has put the collective intelligence of the masses to a very good use and which continues to be a must-know-of tool for everyone. This means lots of people use it. This means bandwith. And lots of it. And servers. And looots of storage. Which in the end means money.

Wikipedia is not advertising supported in any way. The Wikimedia foundation raises money to keep the project going. In a previous article (don’t remember the source) one representative of the foundation claimed that they gather $1million and they should be OK with finances.

So what will become of wikipedia? Will it introduce ads to support itself or continue to rely on donations? Either way I hope that it does not end here. There is still a lot of information that needs to become available.

FABridge & memory …

One of the biggest changes in the latest FABridge release is the addition of memory management mechanisms. If until now everything that was passed from ActionScript to JavaScript via the bridge stayed in memory until the page was reloaded, this is true no more. now various objects behave differently with their allocated memory, depending on their origin and purpose.

The mechanism behind the memory management functions is basic reference counting, without cycle handling. This is enough because both ActionScript and JavaScript already have their garbage collection schemes in place, so all that was needed was a way to remove object references from the bridge structures. And this has been put in place. If you don’t care about the memory you are using, then you can develop applications almost just like now (there’s a single thing to chase here). If you do care, the bridge offers the tools to help:

  • Increasing the reference count for an object: object.addRef() or the more arcane FABridge.addRef(object). Useful when an object is destroyed automatically but you still need it. (Special hint here for those who do not care about memory – you still need to do this, or you’ll end up with stale references).
  • Decreasing the reference count for an object object.release() or FABridge.release(object). Do this when you care about memory and want to clear out old objects. If the reference count has been artificially increased above 1, this call will not destroy the object. If it is already 1, the object will be destroyed immediately.
  • FABridge.releaseNamedASObject(object) -immediately destroy the object passed as argument, regardless of its reference count.

As you can see a lot has been going on when it comes to memory, with emphasis on helping the user and keeping it simple. These changes mean that now some objects clean themselves up when they are no longer needed, but not all. And as such, if you want to avoid errors triggered by null object references you should understand what is kept and what not.

The following rules apply:

  1. Objects created explicitly by the user from JavaScript are not removed from the bridge automatically.
  2. Objects creates as the result of an ActionScript function call and returned to the user in JavaScript are kept. Their intended purpose is not obvious, so we cannot remove them automatically.
  3. New objects created in ActionScript after being passed as arguments to function / method calls from JavaScript are not automatically destroyed.
  4. Events and other objects created automatically in ActionScript and passed to a handler function in Javascript only live for the life of their dispatch function. Outside its scope, the object reference no longer exists.

One of the most basic examples which will appeal to both developers which care and which don’t care about memory is the case when you add an event handler from JavaScript and need to delay an execution via a setTimeOut call, like here:

….
var flexapp = FABridge.flash.root;
flexapp.myButton.addEventListener(“click”,jsDispatchFunction);
…..
function jsDispatchFunction(event) {
setTimeout(function() {doSomethingLater(event);},10);
}
…..
function doSomethingLater(event) {
alert(event.getKind());
}

This snippet of code, as it is right now will fail, because once the direct dispatch function ends, the event object is destroyed. Thus, the doSomethingLater function will throw an error. If you add a line inside the jsDispatchFunction, like this

event.addRef()

will fix everything.

And this is it for this post, for it is already too long!

A new version of FABridge is out

… quite some time now actually (from last Tuesday). In case you don’t know already, the Flex-AJAX Bridge (or the much shorter name FABridge) is a library that allows developers integrate Flex applications with JavaScript code. It is a way to access and use Flex components directly from JavaScript code, without duplicating functionality. It allows accessing objects and functions in the target flash movie on the page, and even registering JavaScript functions as event listeners for flash events.

Getting started …

To make a Flex app available for JavaScript integration via the FABridge you will have to place the FABridge.as file in your project and add a component tag in the application, like this:

<bridge:FABridge bridgeName="flash" id="flash" />

Next make sure you give all of the visual components that you want to access from JavaScript an unique ID (this is something you should do all the time though).

Let’s make it clear, and use an example: we want to have for this demo a Flex component which displays a ‘Hello world’ text and a button which refreshes it. On the JavaScript side we’ll simply have a text box where you can type the message you want displayed in the Flash movie. Also, to showcase event handling, when the refresh button is clicked, a JS alert box will pop up. This is how the end page will look like this:

FABridge sample preview

First step – building the Flex component. It’s quite easy actually:

  1. To create a simple application just like this fire up your text editor of choice (yeah, you heard me right, no need for Flex Builder here) and paste the following code in:
    <?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute" width="220" height="220">
    
    <bridge:FABridge bridgeName="flash" id="flash" />
    
    <mx:Label x="10" y="25" text="Hello world!" width="157" height="26"
    fontSize="12" fontFamily="Arial" id="myLabel"/>
    
    <mx:Button x="59.5" y="80" label="Refresh" id="myButton"
    click="myLabel.text='Hello world reloaded!'"/>
    
    </mx:Application>
  2. Now save the new file – let’s say helloworld.mxml.
  3. Now copy the FABridge.as file from the src folder within the zip you’ve downloaded from here into a folder called bridge on the same level as your mxml file.
  4. Now open a command prompt and browse to the location of your project. Once there call the mxml compiler – mxmlc.exe. You can find it in the bin folder of your Flex SDK installation.
  5. At this point you should have a swf file named helloworld.swf in the same folder as your mxml application.

Step 2 – write the HTML page – it’s just as easy:

  1. Fire up that preferred editor again. Punch in the HTML to create the text box and to load the swf file.
  2. To enable the web page to interact with the embedded Flex application you will have to add an import for the FABridge.js:
    <script src="javascript/FABridge.js"></script>
  3. To pass the text in the input text box to the flash movie we need to write a JavaScript function – relax, it’s easy, two rows are enough:
    function setText()
    {
       var flexApp = FABridge.flash.root();
       flexApp.getMyLabel().setText(jsMessage.value);
    }
  4. Now add the function call to the blur event of the text box and it’s done. Once you type and tab out of the text box, the message will appear inside the Flash movie, just like this:
    Messages from JavaScript
  5. For the last touch, let’s register a JavaScript function as an event handler for when the flash button is pressed – this will take some more code, about 4 lines 🙂
    function initListener()
    {
       var initCallback = function()
    {
       var flexApp = FABridge.flash.root();
       flexApp.getMyButton().addEventListener("click",textChanged);
    }
       FABridge.addInitializationCallback("flash",initCallback);
    }function textChanged(event)
    {
       alert("The text has been reset from Flash");
    }
  6. The textChanged function contains the code which gets executed when the button is pressed. You can do whatever you want in this function. The first function should be called by the body’s onload event and will register an event handler on the Flex button – when clicked, trigger the JS function.

With this simple example, you can see how the FABridge makes Flash – JavaScript communication easier. With just a few lines of code you can control a Flex component without going through the ExternalInterface hassle. You can download the sample application created in this example here.

Now that you know what FABridge does, next post will be about the changes in the latest version. Until then, have fun mixing Flex and AJAX!