如何触发观察者通知,并使侦听器位于另一个 XUL 窗口上
我是 XUL 编程的新手,我学到了很多东西,但我很难理解“触发观察者通知”和“触发观察者通知”。让另一个 XUL 窗口上的侦听器从观察者函数获取字符串值(观察者将字符串参数传递给其他 XUL 窗口)。
这是我的树 xil 文件。
<?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>
这是我的脚本:
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");
}
这是我的文本框 XUL 文件。我想将 xul 树单元格值触发到此文件中的文本框。
<?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>
我在将树单元值发送到另一个 XUL 窗口时遇到问题。从我的文本框 XUL 文件中,我可以打开我的 Tree XUL 文件,当我单击树单元格时,我可以获得树单元格的值作为警报消息。之后我的脚本就不起作用了。
基本上,当我单击树单元格时,所选的树单元格值应该触发到另一个 XUl 窗口中的文本框。
请有人帮我解决这个问题。这里我将 scrip 文件和 xul 文件分开。 我从这个网站上获取了所有可能的脚本。https://developer.mozilla.org/en/ XUL_School/Observer_Notifications 谢谢。 注意:我在同一篇文章中更新了我的问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您发布的代码中,您在添加观察者后定义了 testObserver,这意味着观察者不会调用您的观察函数,而是调用未定义函数的观察函数...
您应该在添加观察者之前定义它,或者使用 testObserver.observe = function(){} 定义观察函数,
但为了更简单地使用观察者,我将使用此文件:
http://code.google .com/p/songbird-telescope/source/browse/trunk/modules/Observers.js?r=2
这是 nsIObserver 的包装器。
您只需要导入文件,该文件在要发送和观察通知的两个文件中都有一个模块:
您可以使用以下方式观察通知:
然后,每次执行以下行时,都会调用 myCallback,并将参数ForMyCallback 作为参数:
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: