Home
Developers

Developers

  • Documentation
    • Developer Guide
    • API Reference
    • Style Guide
    • How-To
    • Troubleshooting
  • Submit a Flake
  • Support

Pageflakes How-To and Troubleshooting Guide

  • Manage Flake Instances
  • Server-Side Scripting
  • Auto-Save Settings
  • Storing an Array into a Profile
  • Using CSS
  • Access Web Services from Flakes
  • Flake Development Tools
  • Troubleshooting
Home » Documentation » How-To

Manage Flake Instances

A flake may appear more than once on a page. As the Pageflakes framework loads each flake onto the page, it must manage each flake instance separately.

For example, consider the following flake:


<html>
  <head>
    <title>HelloWorld</title>
    <script type="text/javascript">
    function onDoSomething()
    {
      var foo = getElementById("bodytext");
      foo.manipulate();  // imaginary method
    }
    </script>
  </head>
  <body>
    <div id="bodytext">
      Hello World
    </div>
  </body>
</html>

If we tried to load this flake twice, we would get two elements with the id of bodytext. The function onDoSomething() would not work correctly for each flake, because getElementById would always return the <div> element of the first flake.

So we have to add a bit more code to handle this scenario:


<html>
  <head>
    <title>HelloWorld</title>
      <script id="com.pageflakes.HelloWorld2"
              type="text/javascript">
          function com_pageflakes_HelloWorld2(id)
          {
            this.load
  = function(flakeInstance)
            {
            }
          }
      </script>
      <script id="_PAGEFLAKES_Instance" 
              type="text/javascript">
        var
  _PAGEFLAKES_ = new HelloWorld('_PAGEFLAKES_');
      </script>
  </head>
  <body>
      <div id="_PAGEFLAKE_bodytext">
        Hello World
      </div>
  </body>
</html>

When the Pageflakes framework loads each flake for a given page, it replaces _PAGEFLAKES_ with an instance identifier, such as m123. Each flake has a different identifier, such that no two flakes on a given page conflict.

The resulting flake that gets added to the page looks like this:


<html>
  <head>
    <title>HelloWorld</title>
      <script id="com.pageflakes.HelloWorld2"
              type="text/javascript">
          function com_pageflakes_HelloWorld2(id)
          {
            this.load = function(flakeInstance)
            {
            }
          }
      </script>
      <script id="m123Instance" 
              type="text/javascript">
          var m123 = new HelloWorld('m123');
      </script>
  </head>
  <body>
    <div id="m123bodytext">
      Hello World
    </div>
  </body>
</html>

If the same flake is loaded again with a different identifier, there is no id attribute collision.

There is one exception to this where id is com.pageflakes.HelloWorld2. In this instance the framework is clever enough to load that script only once, since it is not needed multiple times.

‹ Pageflakes How-To and Troubleshooting GuideupServer-Side Scripting ›
»
  • Printer-friendly version
  • Add new comment