gpt4 book ai didi

javascript - 如何触发观察者通知,并在另一个 XUL 窗口上设置监听器

转载 作者:行者123 更新时间:2023-11-30 06:03:30 25 4
gpt4 key购买 nike

我是 XUL 编程的新手,我学到了很多东西,但我很难理解触发观察者通知并让另一个 XUL 窗口上的监听器从观察者函数中获取字符串值( observer 将字符串参数传递给其他 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 文件中,我可以打开我的树 XUL 文件,当我单击树单元格时,我可以获得树单元格的值作为警告消息。在那之后我的脚本不工作了。

基本上在这里,当我单击树单元格时,选定的树单元格值应该触发到我在另一个 XUl 窗口中的文本框。

请有人帮我解决这个问题。在这里,我将 scrip 文件和 xul 文件分开了。我从这个网站上获取了所有可能的脚本。 https://developer.mozilla.org/en/XUL_School/Observer_Notifications谢谢。注意:我在这里的同一篇文章中更新了我的问题。

最佳答案

在您发布的代码中,您在添加观察者之后定义了 testObserver,这意味着观察者不会调用您的观察函数,而是调用未定义函数的观察函数...您应该在添加观察者之前定义它,或者使用 testObserver.observe = function(){}

定义观察函数

但是为了更简单地使用观察者,我会使用这个文件:
http://code.google.com/p/songbird-telescope/source/browse/trunk/modules/Observers.js?r=2
这是 nsIObserver 的包装器。
您只需要导入文件在您要发送和观察通知的两个文件中都有一个模块:

 Components.utils.import('resource://modules/Observers.js');

您可以使用以下方式观察通知:

Observers.add('myTopic', myCallback, myCallbackSubject);

然后,每次执行以下行时,都会调用 myCallback,并将 parameterForMyCallback 作为参数:

Observers.notify('myTopic',parameterForMyCallback);

关于javascript - 如何触发观察者通知,并在另一个 XUL 窗口上设置监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6760613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com