A Quick Note On Pub-Sub / Event Aggregators In WinJS/WinRT

By now you know that I’m a fan of pub-sub / event aggregators, whether it’s .NET / Winforms or Backbone or whatever. For the last 3 weeks working on this WinJS / WinRT app with the Microsoft P&P group, I’ve had this thought that I want to do the same thing in WinJS / WinRT apps. Only I couldn’t figure out how. @Bennage and I had several discussions on this and decided to look in to it later. Well today was “later” apparently, and thanks to the Chris Tavares I found these wonderful little nuggets of awesome:

WinJS.Application.queueEvent – queue an event to be sent

WinJS.Application.addEventListener – handle an event that was sent

function RaiseEvent(){

  // queue the event to be raised as an object literal
  WinJS.Application.queueEvents({

    // the "type" is the event name
    type: "myEvent",

    // the "detail" is the event args to pass along
    detail: {
      my: "event args",
      go: "here"
    }

  });
}

// subscribe to the "myEvent" event
WinJS.Application.addEventListener("myEvent", function(args){

  // the "args" argument is the object you queued

  args.type; //=> "myEvent"
  args.detail.my; // => "event args"
  args.detail.go; // => "here"

});

RaiseEvent();

Of course this is only an application level event aggregator. If I want to use localized / sub-application / module event aggregators, I need to find another way to make that happen. I have some leads on this, but haven’t had a chance to work on them yet. I have hope, though, and this is yet another reason that I really like working on WinJS apps.


Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs SignalLeaf.com - the amazingly awesome podcast audio hosting service that everyone should be using, and WatchMeCode.net where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in Design Patterns, Javascript, WinJS. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://twitter.com/EisenbergEffect Rob Eisenberg

    Since you are already a big Backbone user, why wouldn’t you just use Backbone events? Did you have a constraint where you couldn’t use third party libraries?

    • http://mutedsolutions.com Derick Bailey

      In the current project, our goal is to create a sample app that provides core guidance on how to build a WinJS app with as few external libraries as possible. The app itself doesn’t use anything 3rd party – only the test suite does.

      In the future (after this project), I want to explore what WinJS + Backbone would look like and see if that would really bring any value to the table, where it would bring value, etc. Of course all of that depends on how much free time I have :)

  • Dave Stibrany

    Hey Im also a fan of using pub/sub. Whats the advantage of having localized event aggregators rather then just one at the app level. Is it just to avoid event name collisions?

    • http://mutedsolutions.com Derick Bailey

      name collision is one reason, but a small one. more importantly: reduce dependencies between objects that can legitimately know about each other because of some other relationship they already have, and reduce memory leaks in some situations by allowing the event binding / triggering to be garbage collected with the objects that trigger and respond to the events. lastly, the semantics introduced with localized pub/sub allows the app to be modularized in a more re-usable manner. you can take a module with you, without being forced to take the global dependency with you.