繁体   English   中英

如何触发观察者通知,并让另一个 XUL window 上的侦听器

[英]How to fire an observer notification, and have the listener on the other XUL window

我是 XUL 编程的新手,我学到了很多东西,但我很难理解触发观察者通知并让另一个 XUL window 上的侦听器从观察者函数中获取字符串值(观察者传递一个其他 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 window 时遇到问题。 从我的文本框 XUL 文件中,我可以打开我的 Tree XUL 文件,当我单击树单元格时,我可以获得树单元格的值作为警报消息。 之后我的脚本不起作用。

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

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

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 = 函数(){}

但是为了更简单地使用观察者,我会使用这个文件:
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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM