WinJS自定义控制事件"无效参数"

时间:2015-06-18 19:16:09

标签: javascript javascript-events winjs

我正在使用事件监听器创建自定义WinJS控件。为简单起见,此示例应在触发事件时触发事件。

这是使用标记创建的:

<div class="alphaNavBar" data-win-control="MobMan.Controls.AlphaNavBar"></div>

此处实施控制。它会抛出一个无效的参数&#34; dispatchEvent(...)行的异常。

(function () {
    var alphaNavBar = WinJS.Class.define(function (el, options) {
        // Create control
        var self = this;
        this._element = el || document.createElement("div");
        this._element.winControl = this;

        this._element.innerText = "Hello World!";
        this._selection = false;

        // Listen for tap
        this._element.addEventListener("MSPointerDown", function (evt) {
            // Toggle selection
            self._selection = !self._selection;

            // Selection changed, fire event
            // Invalid argument here
            self._element.dispatchEvent("mySelectionChanged", { selection: self._selection });
            // Invalid argument here
        });
    });

    // Add to global namespace
    WinJS.Namespace.define("MobMan.Controls", {
        AlphaNavBar: alphaNavBar
    });

    // Mixin event properties
    WinJS.Class.mix(MobMan.Controls.AlphaNavBar, WinJS.Utilities.createEventProperties("mySelectionChanged"), WinJS.UI.DOMEventMixin);
})();

此事件由以下人员收听:

var alphaNavBar = document.querySelector(".alphaNavBar");
alphaNavBar.addEventListener("mySelectionChanged", function (evt) {
    // Should fire when alphaNavBar is tapped
    debugger;
});

我在这里做错了什么?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我也发布了我的问题here并得到了修改事件发送的答案,如下所示:

// Listen for tap
this._element.addEventListener("MSPointerDown", function (evt) {
    // Toggle selection
    this._selection = !this._selection;

    // Create the event.
    var _event = document.createEvent('customevent');

    // Define that the event name is 'mySelectionChanged' and pass details.
    _event.initCustomEvent('mySelectionChanged', true, true, { selection: this._selection });

    // Selection changed, fire event
    this.dispatchEvent(_event);
});

这能够为我正确触发事件。还不确定我以前做错了什么,但现在已经修好了。