document.loadBindingDocument('chrome://package/content/myBindings.xml');
var listbox = document.getBindingParent(this); var cellValue = listbox.childNodes[3].firstChild.label;
Returns an array with the input binding's top-level content nodes. Refer to the section Section 7.4.3, later in this chapter, for more details.
Returns a single anonymous element when passed an element, an attribute from that element, and its value. Refer to the section Section 7.4.3 for more details.
The section Section 7.2 covered the attachment of a binding to a bound element using CSS. This technique is the most common method, but you can also attach bindings with the addBinding method.
Using this method as an alternative to CSS attachment is useful when you do not want to attach a binding in all circumstances. In an application based on user input, you may not want to load a binding until certain values are entered. For example, in a membership database, the information that appears on screen may depend on the user's level of membership. The following snippets show how it is used.
<mybinding id="myNewWidget" class="attached" />
To load a binding, add these two lines in your script.
var binding = document.getElementById("myNewWidget"); document.addBinding(binding, "chrome://mypackage/content/myBindings.xml#super");
Notice that the URL used to access the binding takes the same format as in the CSS property -- i.e., the path to the file and the id of the binding qualified by #.
This example shows how to remove a reference to a binding by resetting it to an empty reference:
mybinding.attached { -moz-binding : url("mybindings.xml#my-binding"); } mybinding.unattached { -moz-binding : url(""); }
When you want to detach the binding from an element, you can do this:
var mywidget = document.getElementById("binding1"); mywidget.setAttribute("class","unattached");
![]() | -moz-binding:url("") can be used at this time as a hack around the -moz-binding:none binding. The later binding does not currently work in Mozilla. |
var binding = document.getElementById("myNewWidget"); document.removeBinding(binding, "chrome://mypackage/content/myBindings.xml#super");
In the case of an inheritance chain (see the Section 7.5 section later in this chapter for more details), the bindings are destroyed from the bottom upwards. This means that if there is tear-down code in the form of a destructor, it is executed last on the base binding.
![]() | While higher-level nodes can be accessed from anonymous content, parents do not have explicit access to their anonymous children using the DOM childNodes property. Using firstChild or nextSibling will also not work. |
Example 7-4 illustrates both properties in use.
Example 7-4 is a binding with two buttons, each of which brings up an alert when activated. The alert simply shows the name of an element that is accessed in the code attached to the button. In Button A, the parent node is the containing box. One level further is the bound element, <mybinding> -- the parent node of the box parent. The alert dialog raised by the alert shows "mybinding." Once a binding is applied, the binding's owner (ownerDocument) is the bound document. Assuming that Button B is a XUL window, the alert, when activated, shows "window." This property can be used to access properties of the document object.
<getter> <![CDATA[ var list = document.getAnonymousNodes(this)[0]; return list.selectedItem.getAttribute('label'); ]]> </getter>
<binding id="my-binding"> <content> <xul:vbox> <children /> </xul:vbox> </content> </binding>
<mybinding id="myNewWidget" flex="1" class="attached"> <label value="this is child 1" /> <label value="this is child 2" /> </mybinding>
Sometimes multiple siblings are located within a box in the XUL, but you want to use only some of them in the binding, such as when a user logs into a system and content is displayed depending on its level of membership. In these cases, you can be selective about which children should be included in the binding. Example 7-5 shows how to use the includes attribute on the <children> element.
The children element in Example 7-5 essentially tells, "Of all the content contained in the bound element, insert only the image element at this particular insertion point." Here is the XUL code that goes with this example:
<mybinding id="myNewWidget" flex="1"> <image src="http://www.mozdev.org/sharedimages/header.gif" /> <label value="a non includes element" /> </mybinding>
The image is the only child taken from the XUL content and the label is ignored.
If you have children that are not defined in the includes attribute, then the binding is discarded and not used. If the bound element uses another element in addition to an image element, the binding is discarded and only the explicit content is used. If the image element isn't used at all, the binding is discarded.
<mybinding id="myNewWidget" flex="1"> <image src="http://www.mozdev.org/sharedimages/header.gif" /> <label value="an element" /> </mybinding>
This example renders the image and the label and discards the binding. The anonymous content does not appear because the binding is discarded and only the explicit content is used.