Skip to content

Collectionextension

maxkfranz edited this page May 1, 2012 · 9 revisions

Collection extensions specification

Introduction

Collection extensions are a mechanism of adding third-party functions to collections.

You must register collection extensions before creating instances of Cytoscape Web. Otherwise, the function will not be there.

Instructions

You can add a function to collections via the extension registration API:

$.cytoscapeweb("collection", "someFunctionName", function(){
  // this function defines eles.someFunctionName
});

It's important to note that when writing your own collection extension, it should be in its own file so it is reusable:

// jquery.cytoscapeweb.collection.someFunctionName.js
(function($){

  $.cytoscapeweb("collection", "someFunctionName", function(){
    // this function defines eles.someFunctionName
  });

})( jQuery );

Example

Here is an example that adds a function named weight that calculates the aggregate weight (stored as data) of a collection of elements.

(function($){ $.cytoscapeweb("collection", "weight", function(){
  var total = 0;

  this.each(function(){
    var w = this.data("weight");
    
    if( w != null ){
      total += w;
    }
  });

  return total;
}); })( jQuery );

You can use the function in your application code as follows:

// somewhere in the app...

console.log( "The selected nodes have a total weight of %s", cy.nodes(":selected").weight() );

Clone this wiki locally