addon.port.on没有收到来自插件的消息

时间:2014-05-28 23:19:21

标签: javascript firefox-addon firefox-addon-sdk

main.js内,我创建了一个Panel ui.html包含js源文件,其中我会收听来自Panel的邮件 我从未看到restoring转储控制台,为什么这个函数从未被调用过?

panel = PanelAPI.Panel({
        width: 300,
        height: 400,
        contentURL: Data.get("html/ui.html")
    });

    panel.port.emit('previousHistory', SimpleStorage.getHistory(), SimpleStorage.getCurrentHistoricalEntry());

    panel.port.on("historyUpdate", function (history, currentHistoricalEntry) {
        SimpleStorage.setHistory(history);
        SimpleStorage.setCurrentHistoricalEntry(currentHistoricalEntry);
    });

contentURL: Data.get("html/ui.html")js文件包含此内容以收听消息..

addon.port.on("previousHistory", function(history, currentHistoricalEntry) {
    console.log("restoring");
    Namespace.restoreHistory(history, currentHistoricalEntry);
});

SimpleStorage.js,是我处理simple-storage api ..

的访问权限的地方
var ss = require("sdk/simple-storage");

exports.getHistory = function(){
    console.log("retrieving ss: " + ss.storage.history)
    return ss.storage.history;
}

exports.setHistory = function(history){
    ss.storage.history = history;
    console.log("history in ss is now: " + ss.storage.history)
}

exports.setCurrentHistoricalEntry = function(currentHistoricalEntry){
    ss.storage.currentHistoricalEntry = currentHistoricalEntry;
    console.log("currhisent is now" + ss.storage.currentHistoricalEntry);
}

exports.getCurrentHistoricalEntry = function(){
    console.log("retrieving ss.currentr: " + ss.storage.currentHistoricalEntry);
    return ss.storage.currentHistoricalEntry;
}

1 个答案:

答案 0 :(得分:1)

this.port.emit('previousHistory', prefs);移至Panel.onShow()工作..

panel = PanelAPI.Panel({
        width: 300,
        height: 400,
        contentURL: Data.get("html/ui.html"),
        onShow: function() { 

            var prefs = JSON.stringify({
                history: SimpleStorage.getHistory(),
                currentHistoricalEntry: SimpleStorage.getCurrentHistoricalEntry()
            });

            this.port.emit('previousHistory', prefs);
        }
    });
相关问题