JZ Publish/Subscribe Version 1.2 Released

It’s a great day here at Joe Zim’s JavaScript Blog and surely a great day for the users of the JZ Publish/Subscribe jQuery plugin. This plugin has now been updated to version 1.2 and has gained probably the best feature it could gain at this point in its life and something that, arguably, maybe should have already been included with the plugin.

In this release we introduce context. Now, instead of all callback functions being run within the context of the jQuery object, you can pass in an optional 3rd parameter to the $.subscribe function that will define the context in which the callback is executed. This means the you can now use the this keyword within your callback functions and know it’ll be the object you want it to be. Also, now when no context is given, a blank object ({}) will be used as the context.

JZ Publish/Subscribe Documentation and Download

The JZ Publish/Subscribe Project Page has already been updated with new documentation and a link to download the new version. You can see an example of how to use this feature in the example code on the project page or by looking below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var obj = {
variable: 1,
func: function() {
// reference a variable via this
this.variable++;
}
}

/**
* Old way without a context parameter.
* This will not get the desired result because
* `func` will be called on a blank object like so:
* `func.call({}, topic, data);`
*/
jQuery.subscribe('test', obj.func);

/**
* New way with a context parameter.
* Now `func` will actually update the `obj.variable`
* property because `this` refers to `obj`
*/
jQuery.subscribe('test', obj.func, obj);

I sincerely hope that you enjoy this new feature and that it is helpful in the development of your JavaScript. If you make a cool JavaScript app using this, you should send me a link via the Contact Me page so I can see. I might even show it off to the rest of the readers in a post, with your permission. Anyway, have a great rest of your weekend (or week if you don’t read this until after the weekend is over) and Happy Coding!

Author: Joe Zimmerman

Author: Joe Zimmerman Joe Zimmerman has been doing web development ever since he found an HTML book on his dad's shelf when he was 12. Since then, JavaScript has grown in popularity and he has become passionate about it. He also loves to teach others though his blog and other popular blogs. When he's not writing code, he's spending time with his wife and children and leading them in God's Word.