在preference.reset()之后访问onsyncfrompreference中的正确首选项值

时间:2011-01-02 12:26:43

标签: firefox-addon xul

我希望在我的分机的首选项窗口中有一个“恢复默认值”按钮:

<prefpane id="nextplease.phrases" label="&options.phrases.title;"
          image="chrome://nextplease/skin/Document.png">
    <preferences>
        <preference id="nextphrases" name="nextplease.nextphrase.expr0"
                    type="unichar"/>
        ...
    </preferences>
    <hbox flex="1">
        ...
                <listbox id="nextphrases_list" seltype="multiple" flex="1"
                    onkeypress="nextplease.removeSelectedOnDelete(event, this);"
                    onselect="nextplease.enableDisableRemoveButton(this);" 
                    preference="nextphrases" preference-editable="true" 
                    onsynctopreference="return nextplease.syncListbox(this);" 
                    onsyncfrompreference="nextplease.loadListboxPreference(this);"/>
                ...
                <button label="&options.restoreDefault;"
                    oncommand="nextplease.restoreDefaultListbox(this);" />
            </hbox>
        </vbox>
    </hbox>
</prefpane>

这是函数的JavaScript代码:

nextplease.restoreDefaultListbox = function (node) {
    var listbox = node.parentNode.parentNode.firstChild.selectedPanel;
    var preferenceId = listbox.getAttribute("preference");
    var preference = document.getElementById(preferenceId);
    preference.reset();
    alert("preference.value="+preference.value+"; preference.valueFromPreferences="+preference.valueFromPreferences);
    preference.setElementValue(listbox);
};

nextplease.loadListboxPreference = function (listbox) {
    var i, phrase, values;

    var preferenceId = listbox.getAttribute("preference");
    var preference = document.getElementById(preferenceId);
    var prefValue = ??? // neither preference.value nor preference.valueFromPreferences work, see below
    // alert("Loading: "+prefValue);

    // remove all items already in listbox
    for (i = listbox.itemCount; i >= 0; i--) {
        listbox.removeItemAt(i);
    }

    values = prefValue.split("|");
    for (i = 0; i < values.length; i++) {
        phrase = values[i].replace(/&pipe;/g, "|");
        if (phrase !== "") {
            listbox.appendItem(phrase, phrase);

            // Scroll to the newly added item to workaround 
            // what I think is a Firefox bug. I was getting
            // Javascript exceptions when trying to read the
            // values at the bottom that are "hidden".
            listbox.ensureIndexIsVisible(listbox.getRowCount() - 1);
        }
    }
    listbox.ensureIndexIsVisible(0);
};

现在,当我点击按钮时,我得到的警报(在preference.reset()之后)看起来像这样:

  

preference.value =未定义; preference.valueFromPreferences =旧的用户设置首选项值

所以在onsyncfrompreference中似乎无法访问正确的首选项值,除非是以一种非常丑陋的方式:

if (preference.value === undefined) {
  prefValue = preference.preferences.defaultBranch.getIntValue(preference.name); // or other type instead of Int
} else {
  prefValue = preference.value;
}

有更好的选择吗?具体来说,哪个适用于任何偏好类型?

1 个答案:

答案 0 :(得分:1)

如果pref.value === undefined,您应该使用pref.defaultValue来获取要显示的值。我更新了参考页面,并the onsyncfrompreference documentation说明了这一点。