1

I am using Dojo 1.9 for a web application I'm writing using WebSockets. When the client receives a message from the server, I need to update certain widgets with the data received.

// sock is the client-side of the websocket
sock.onmessage = function (dataIn) {
    // clientManager defined elsewhere
    clientManager.fireMessageReceived(dataIn);
};

Here's my problem: At the point that I receive the data, I don't have the ID's, DOM nodes or widgets to access the properties/values to be updated. clientManager deals specifically with sock events and doesn't have any specific knowledge of the widgets that its data will be updating. Also, it's possible to have multiple instances of the same widget, so I think trying to maintain a collection of existing widgets (or ID's) as a property of the client manager could get hairy pretty quickly.

So, my solution was to use CSS classes.

I created an empty class and assigned it to my widget:

.myXYZWidget {

}

so that in my fireMessageReceived function, I can use dojo/query to find it:

var myXYZWidgets = dojo.query(".myXYZWidget");
var i;

for (i = 0; i < myXYZWidgets.length; i++) {
    var xyzWidget = registry.byNode(myXYZWidgets[0]);

    ... // Now I have my Dojo widget, I can upate to my heart's content
}

This works and I'm not seeing any major downsides to doing this, but is this ok or is this bad bad bad? Can anyone in the community that has a knowledge of Dojo confirm this solution or suggest a better one?

2
  • It might help to fill out some details on the over design where these widgets are created and kept track of. As you mentioned the css class approach works, but it has a bit of "code smell". Also, there is no need to have the actual empty class in your CSS. Jul 5, 2013 at 15:44
  • @BuffaloBuffalo, I updated the question. If you need more information, I'll update again.
    – David
    Jul 5, 2013 at 15:58

1 Answer 1

2

A class is not necessarily CSS. So you're not using CSS to get Dojo widgets, you're just accessing the widgets by a selector/query.

If I think about your problem I would think about the publisher/subscriber pattern where your websocket is your publisher (since it receives data and needs to emit it to your widgets) and your widgets are your subscribers.

Subscriber(s) (widgets)

Luckily for you dojo has something for it and it's called the dojo/topic module. When you create your widget, you somehow want to make sure that it's a subscriber of the data your websocket receives. To do that, I would do something like this (I'm using a dijit/form/Select in my example, but you can rewrite it to whatever you want):

lang.mixin(mySelect, {
    __getData: function(data) {
         this.addOption(data);   
    }
});
mySelect.own(topic.subscribe("my/event", lang.hitch(mySelect, '__getData')));

What happens here is pretty easy (altough it might look hard). The first thing I do is to make sure my widget has an extra method called __getData. This method will receive the data from the websocket and will update its own based on the data.

Then I make sure the widget is subscribed to events with the name my/event (you will see what this means in a while).


Publisher (websocket)

Then at the level of your websocket code, you want to publish to a topic called my/event so that your widgets will know about it.

You can do that like this:

topic.publish("my/event", myData);

Where myData is the data you received from your websocket.


So now the flow is complete. Your websocket code will emit data to who wants to listen. The listeners (widgets) will use the data and do something with it, for example adding items to themself.

I also made a JSFiddle showing a complete example. This solution might look more complex, but I think it's pragmatically more correct.

1
  • Now why didn't I think of that?! :) Thanks!
    – David
    Jul 5, 2013 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.