Skip to main content link. Accesskey S
  • Anonymous
  • Log on
  • Help
  • IBM logo
  • IBM Mashup Center wiki
  • All Wikis
  • Home
  • Community Articles
  • Product Documentation
  • Learning Center


Search

Advanced Search

Categories

Tag Cloud

  • 2.0
  • 3.0
  • 3.0.0.1
  • action widgets
  • actions
  • admin
  • administering
  • administration
  • administrator
  • adminstering
  • AJAX
  • AJAX proxy
  • annotate function
  • annotator
  • API
  • api reference
  • benchmark testing
  • catalog
  • configuing
  • configuration
  • configure
  • configuring
  • creating
  • creating mashups
  • data
  • data mashup builder
  • data mashups
  • database
  • DB2
  • demo
  • deploy
  • deployment
  • designing applications
  • developer
  • developing
  • developing mashups
  • developing widgets
  • documentation
  • ECM Widgets
  • editing
  • Enterprise Content Management Widgets
  • errors
  • events
  • examples
  • explanations
  • feed
  • feed mashups
  • feeds
  • format
  • functions
  • getting started
  • greenhouse
  • guide
  • IMS database feeds
  • IMS feeds
  • IMS transaction feeds
  • installation
  • installing
  • mashup
  • MashupHub
  • mashuphub examples
  • mashuphub users guide
  • mashups
  • messages
  • migrating
  • objects
  • operators
  • Oracle
  • pages
  • payload types
  • pdf
  • performance
  • performance tuning
  • planning
  • product
  • programming
  • proxy
  • rest services
  • security
  • service
  • spaces
  • themes
  • troubleshooting
  • tutorial
  • tutorials
  • upgrading
  • user
  • users
  • v1.0
  • v1.1
  • v2.0
  • v2.0.0.2
  • v4.5.2.1
  • video
  • WAR
  • widget
  • widgets
  • widgets
  • wire
  • YouTube
InformationInformation
You are currently viewing machine translated content. IBM translation might be available. Click IBM Translated Product Documentation to see what is available.X


Home > Developing > Adding lifecyle functions to a Mashup widget to load and unload the widget’s CSS
Rate this article 1 starRate this article 2 starsRate this article 3 starsRate this article 4 starsRate this article 5 stars

Adding lifecyle functions to a Mashup widget to load and unload the widget’s CSS 

expanded Abstract
collapsed Abstract
Currently IBM Mashup Centre will load all the files listed as <iw:resource> when the widget gets displayed. But, when the widget gets disposed of, the CSS files that have been loaded for the widget, don’t get removed.
ShowTable of Contents
HideTable of Contents
  • 1 Adding lifecycle functions
To avoid any possible problem with existing CSS files from other widgets overwriting each other, you can add two small functions to the widget’s code, and let the widget control the load and unload of its CSS file(s).
 
These functions override the common usage of the ‘target’ attribute in the link tag, as its role isn’t supported by any browser.

By placing the widgetId as the ‘target’ attribute’s value, we can identify that the link was added by a particular widget, which means we can easily remove it when needed.

Adding lifecycle functions


1. Make sure that you do not list the CSS file(s) as in the widget’s xml file. Comment them out if you do.

2. Edit the widget’s javascript file and add the 2 following functions in the widget’s code.
In this example the CSS file is found in /styles/myWidget.css, change this as needed for your own widget.

loadWidgetStyleSheet: function(){
    var link = dojo.doc.createElement('link');
    link.rel = 'stylesheet';
    link.href = this.iContext.io.rewriteURI("/styles") +'/myWidget.css';
    link.type = 'text/css';
    link.target = this.WID;


    dojo.doc.getElementsByTagName('head')[0].appendChild(link);
},


unloadWidgetStyleSheet: function() {
    var list = dojo.doc.getElementsByTagName('link');


    for (var i=list.length; i >= 0; i--) {
        if (list[i] && list[i].getAttribute('target') == this.WID) {
            list[i].parentNode.removeChild(list[i]);
        }
    }
}, 

3. Call the new functions respectively from onLoad():

onLoad: function() {
    this.WID = "_"+this.iContext.widgetId+"_";
    // load my CSS
    this.loadWidgetStyleSheet();
    ... (load the rest of the widget) ...
}, 
 
... and from onUnload():

onUnload(): function() {
    // remove my CSS
    this.unloadWidgetStyleSheet();
    ... (do more clean up) ...
}, 

 

You should now see your CSS file(s) get unloaded when your widget is being disposed of.

By controlling the loading and unloading of the widget’s CSS file(s), the widget’s author is making sure that the widget’s code and stylesheet(s) will not linger in the Mashup environment after the widget has been destroyed. 

expanded Article information
collapsed Article information
Category:
Developing
Tags:
css, widget, onLoad, onUnload

This Version: Version 5 July 13, 2011 5:13:21 AM by Florence Adam  IBMer

expanded Attachments (0)
collapsed Attachments (0)

 


expanded Versions (5)
collapsed Versions (5)
Version Comparison     
Version Date Changed by               Summary of changes
This version (5) Jul 13, 2011 5:13:21 AM Florence Adam   fix abstract again
3 Jul 13, 2011 5:11:57 AM Florence Adam   remove brackets
3 Jul 13, 2011 5:11:57 AM Florence Adam   change header
2 Jul 13, 2011 5:10:59 AM Florence Adam   remove brackets
1 Jul 13, 2011 5:10:19 AM Florence Adam  
expanded Comments (0)
collapsed Comments (0)
Copy and paste this wiki markup to link to this article from another article in this wiki.
Go ElsewhereStay ConnectedSubscribe to RSSHelpAbout
  • All Lotus and WebSphere Portal wikis
  • IBM developerWorks
  • IBM Software support
  • IBM Social Business User Experience Blog
  • IBMSocialBizUX on Twitter
  • IBMSocialBizUX on Facebook
  • Lotus product forums
  • IBM Social Business UX blog
  • IBM Collaboration Solutions
  • Recently added feedRecently added
  • Recently edited feedRecently edited
  • Recently added comments feedRecently Added Comments
  • Wiki Help
  • Forgot user name/password
  • Wiki design feedback
  • Content feedback
  • About the wiki
  • About IBM
  • Privacy
  • Contact IBM
  • IBM Terms of use
  • Wiki terms of use