cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

Services on Widgets

dclayworth
1-Newbie

Services on Widgets

I understand from browsing the source that you can define services on widgets. Is there any documentation, or specific examples, on how to do this?

Part of the thinking behind this is that I want to be able to do simple transformations on data as it is passed from one widget to another. If I write a service on a Thing to do the transformation, then the data has to go back to the server, be transformed, and then come back to the client. If I can somehow write a service that lives on the client then I can avoid this. Am I on the right track for how I should do this?

1 REPLY 1

That's a perfectly reasonable approach.  You may be able to accomplish it with the expression widget if it's a simple enough transformation.  But presuming you need to do it within a widget, it's very straightforward.

One example you can see in the combined js source is the navigation widget.

In the ide.js portion of your widget, declare the service like this:

this.widgetServices = function () {

  return {

  'Navigate': { 'warnIfNotBound': false }

  };

};

In the runtime.js of your widget

this.serviceInvoked = function (serviceName) {

  var widgetReference = this;

  if (serviceName === 'Navigate') {

    // your code here!

  } else {

  TW.log.error('Navigation widget, unexpected serviceName invoked "' + serviceName + '"');

  }

};

Other notes:

You will need to declare the various properties that you'll be using for your calculations, as services on widgets do not take parameters.   The slider widget provides a good example of this as well.

Top Tags