I’m a newbie in XUL programming and I learned many things but I’m having a hard time understanding in Firing an observer notification & have the listener on the other XUL window to get the string value from the observer function(The observer passes a string parameter to other XUL window).
This is my tree xil file.
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window title="ContactMatrix;"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/x-javascript" src="fire.js" />
<hbox>
<textbox type="search" oncommand="SearchKeyword(this)"/>
</hbox>
<tree editable="false" id="my-tree" flex="1" seltype="cell" onclick="onTreeClicked(event)"
datasources="file://C:/mercredi.xml" ref="*" querytype="xml" enableColumnDrag="true" hidecolumnpicker="false" >
</tree>
</window>
This is my script:
function SearchKeyword(oElem)
{
var filter = document.getElementById("filter");
filter.setAttribute("value", oElem.value);
document.getElementById("my-tree").builder.rebuild();
}
function onTreeClicked(event){
var tree = document.getElementById("my-tree");
var tbo = tree.treeBoxObject;
// get the row, col and child element at the point
var row = { }, col = { }, child = { };
tbo.getCellAt(event.clientX, event.clientY, row, col, child);
var cellText = tree.view.getCellText(row.value, col.value);
alert(cellText);
observe();
}
XPCOMUtils.defineLazyServiceGetter(this, "obsService", "@mozilla.org/observer-service;1", "nsIObserverService");
obsService.notifyObservers(null, "xulschoolhello-test-topic", cellText);
let observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.addObserver(testObserver, "xulschoolhello-test-topic", false);
let testObserver = {
observe : function(aSubject, aTopic, cellText) {
if (aTopic == "xulschoolhello-test-topic") {
window.alert("Data received: " + cellText); //cellText=aData
var textboxElement = document.getElementByID("ali");
textboxElement.value = cellText; // from the notification
}
}
}
var yourAddonObject = {
obsService: Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService),
register: function()
{
this.obsService.addObserver(this, "xulschoolhello-test-topic", false);
}
}
function DisplayContacts()
{
alert('opening file fire');
var windowObjectReference = window.openDialog("chrome://hello/content/fire.xul","fire");
}
This is my text box XUL file. Where i want to fire the xul tree cell values to the text-box in this file.
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<window title="Subjects import"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" >
<script type="application/x-javascript" src="fire.js" />
<groupbox>
<caption>Subject(s)</caption>
<hbox>
<label value="Subjects"/>
<textbox id='ali' value=""/>
<button label="Contacts" onclick="DisplayContacts();"/>
</hbox>
</groupbox>
</window>
I have a problem in firing the tree cell values to my another XUL window. From my text-box XUL file I can open my Tree XUL file and when i click on tree cell I can get the values of the tree cell as a alert message. After that my script is not working.
Basically here, when i click on the tree cell, the selected tree cell values should fire to my text-box in the other XUl window.
Some one please help me to fix this problem. Here I have separated scrip file and xul files.
From this website I have taken all the possible scripts.https://developer.mozilla.org/en/XUL_School/Observer_Notifications
Thank you.
Note: I have updated my question in the same post here.
In your posted code you define testObserver after adding the observer, which mean the observer won’t call your observe function but the observe function of an undefined function…
You should either define it before adding the observer or define the observe function using testObserver.observe = function(){}
But for a simpler use of observer I would use this file:
http://code.google.com/p/songbird-telescope/source/browse/trunk/modules/Observers.js?r=2
This is a wrapper of the nsIObserver.
You just need to import the file has a module in both file where you want to send and observe a notification:
You can observe a notification using:
Then, everytime the following line is executed, myCallback is called having parameterForMyCallback as a parameter: