Calling the install function

The next piece we need to add is the code to install the bar. For this we have to use the sb:GenerateBar method, which is available to us because we specified the library in the functions attribute. We have to call the function from an XPath expression, and since we want the returned value (so that we know if it succeeded or not) we'll use the setvalue action. We'll put the setvalue inside an action handler that is listening for the event my-install-bar; place the following in the model element:

      <xf:instance src="delicious-sidebar.xml" />

      <xf:action ev:event="my-install-bar">
        <xf:setvalue
         ref="@retval"
         value="sb:GenerateBar(
          string(../type),
          string(../internalName),
          string(../name),
          string(../uri)
         )"
        />
      </xf:action>
    </xf:model>

Once the function returns we'll be left with either "Success" or an error message in the retval attribute. We can make use of this by following the setvalue action handler with a message:

        />
        <xf:message level="modal">
          '<xf:output ref="name" />'
          has
          <xf:output value="
            if(
              @retval = 'Success',
              ' been added. You will need to open a new browser
                window to see the change.',
              concat(
                ' failed to install. The error is &#34;',
                @retval,
                '&#34;.'
              )
            )"
          />
        </xf:message>
      </xf:action>

The effect of this is to always show a message after installing, but the text of the message will depend on whether the installation was successful or not--with the if function being used to determine which.

To allow the user to install the bar, all we need do now is provide a button that will invoke the my-install-bar event. We'll put it after the other controls:

    <div><xf:output ref="description" /></div>

    <xf:trigger>
      <xf:label>Install</xf:label>
      <xf:dispatch ev:event="DOMActivate"
       name="my-install-bar" target="m-soft-bars"
      />
    </xf:trigger>
  </body>