Cross YUI Communication and Custom Events

I am constantly impressed by the degree of excellence I find in working with the YUI3 framework. Things that should just “work” actually do. Recently I was stumped by an issue you yourself might be facing if you use YUI to any degree and have to deal with multiple YUI().use() instances on your page. In fact – after working through this short demo – I hope you have the confidence to do this yourself when the needs of the code warrant it, instead of trying to force everything into a single YUI().use() statement.

If you are aware of how YUI3 works, every YUI().use() statement is a self contained bit of javascript, where the scope of all the variables, functions, etc are nicely hidden and protected from the outside “global” javascript. This is a core feature of YUI and it takes a lot to work around this – which I would advise against by the way. So, you may find yourself with more than one of these YUI().use() blocks in your page. Perhaps you are using another library, or there is some legacy code that you don’t have access to, but still need to communicate with. Normally, such communication is not possible due to how javascript deals with scope, but you can work around this very nicely with two YUI features: Y.Global and YUI Custom Events. Let’s get to the code.

In this page we have to boxes. Each box has had a different YUI instance bound to it. They are self contained and for the most part do not know anything about each other. However in each block of YUI we have set a Y.Global.on() call. This essentially says “Go up to the global space set aside by YUI and listen for this event”. Notice however that the scope of the method call is the one that calls Y.Global.on(). As you can tell by the “theInput” variable. It points to the input box of that particular YUI instance. Any other YUI instance can then call a Y.Global.fire() method call. When they do, they can send along arguments that are scoped from that particular different YUI instance. Note that we had to use the “event-custom” component as well, in order for Y.Global to be available.

Another way of thinking about it is imagine you are sitting in your cube with a bunch of ping pong balls. And all of your cube mates have the same thing. Imagine that you cannot leave your cube, nor can you pop your head up above the walls of the cube. You can however lob ping pong balls over the wall to your cubemate. They never see “you” but you can write messages on your ping pong balls that they can read, and in turn do some action, or even lob one back to you in response.

The analogy breaks down when you have more than two cubes, but imagine when you toss that ball out, it magically splits into however many it needs to in order to drop into everyone else’s cube :)

What is nice about this is that you do not have to know a whole lot about the “other” YUI instances. You can safely keep your private methods private, and only provide access to things you want the outside world to know about via the arguments you pass to the fire method. This also is safer than trying to dig your way into another YUI instance directly, because perhaps it hasn’t loaded yet, or it has changed since you wrote this code, and your direct access gets broken.

Anyway, I hope you enjoy this little snippet and I encourage you to check out the YUI docs for Y.Global (API Reference) and Custom Events ( API Reference) .

Links of the week – Feb 23, 2011

Thanks to http://http://www.twitflink.com/ I’m able to grab the links to the sites I’ve tweeted about this past week. Check out things you might have missed:

The Cambrian Explosion of web game engines.

With the advent of HTML5, SVG, CSS3, faster Javascript engines, and other web technologies we are experiencing what you might call a Cambrian explosion in web game engines. If you are unfamiliar with the term, a Cambrian Explosion refers to the period of time in earth’s history when there was a massive uptick in the number of species of animals in our oceans. All kinds of strange and wonderful creatures were rapidly appearing and evolving into still more types of creatures. No one really knows why this happened, except for the fact that the conditions were probably just right for this to happen, and that this change in conditions was a relatively recent event (right before the “explosion”). In terms of web technologies, we are seeing a similar rapid expansion of code libraries, demos, browser updates, and new specifications.

Any one of these events, on their own – such as a new HTML5 spec – would not be enough to drive the type of amazing development we are seeing. Instead, it is the synergy of an open web, open standards like HTML5, CSS3, and SVG, code repositories such as GitHub, and advances in the browser world with faster Javascript engines and hardward Canvas acceleration. I do not believe we can understate the importance of IE9 in all of this as well. IE has been considered the “boat anchor” of the internet – holding back innovation in the browser space – but with the advent of IE9 with tremendous new capabilites as well as serious adoption of web standards, I would venture to say we are entering into a new “Golden Age” of web development.

This presents you with a choice, of sorts. You can choose to become an agent / facilitator of this great explosion of web technologies, or choose instead to become an evolutionary dead end. The pace is only going to increase as we begin to explore the ramifications of what has already been presented to us in the form of new specifications. You can see it already with the idea that HTML will go “versionless”, and browsers will stop halting at major versions and simply update on a monthly or even weekly basis. One of the principles of a chaotic system is that small change in initial conditions can have dramatic effects down the line. You can become one of those “initial conditions” by contributing and using new web technologies. Stop building “demos” and “snippets” and really create something with lasting value with this new toolset.

What does this have to do with web game engines? Well, there is a parallel revolution going on in the game space. Seemingly “retro” games are growing in popularity with the “casual” crowd. Gaming itself is growing more accepted as a normal form of entertainment right along with reading or watching movies. The same web technologies that can make filling out forms more appealing, or watching web video easier can also be applied to creating web-based games. I firmly believe we have hardly scratched the surface of what a web game “could” do – given that it straddles the worlds of gaming, internet connectivity, geolocation, augmented reality, and more.

So this is really a long winded challenge to you to go out there and build superfantastic web games. Learn WebGL or Canvas and pick up a cool engine to work with. More than that, contribute code fixes, examples, and enhancements to your favorite engine of choice. The web is making programming something that anyone with a browser and a text edtior can pick up easily and create powerful applications (and games). Be a part of the web’s Cambrian Explosion and get your little game critters out into the ecosystem where they can grow and evolve. Who knows? Something you create in the late evening hours may someday be the forerunner of the next Big Thing on the web.

Here’s a growing list of game engines, javascript libraries, and other stuff to help you along on your journey. Let me know how it goes!

https://gist.github.com/768272

Build a Simple Ticker in YUI

Kicking off my “Make it with YUI” series, I decided to begin with something classic. Building a “ticker” or a tickertape like element for your site is not too difficult to do without YUI, but if you already have it on your site, or are considering using it for adding more interactivity, you might like this. YUI transitions come to great effect here, and turn this bit of code into a fairly trivial exercise. Click over to the “result” tab to see our ticker in action.

With a bit of tweaking, you can get the speed just right for your site, and the code should be fairly adaptable to varying widths of viewport and content. Basically what we are doing here is creating a div with a set height and width, and setting its overflow to “hidden”. We then set the inner span to be absolutely positioned. Once we have that finished, we simply slide the inner span to the left via the transition – using the inner span’s width. Once the inner span has travelled all the way past the left side of the outer div, we reset the inner span’s “left” attribute to be just to the right of the outer div’s view.
After that, its just a matter of running the animation again with this new starting point, and repeat forever.

I intentionally made the code a bit verbose, but I’m sure you could pare it down quite a bit if you wanted. How did I do? Do you have a smaller, faster, or better scrolling ticker done in YUI? Did I miss something obvious? Let me know!

Links of the week

I post a lot of links each week via twitter, and often I have the feeling that they get lost in the crowd pretty often. Also, it’s nice to have a record of the things I’ve linked to, and I found this really useful site called TwitFLlink that will take a twitter user and grab all the links from them from the past month or so. Perfect!

Here are the links I’ve posted this week: