dojox/mobile/SearchBox 'onSearch' event ru

2019-08-23 04:48发布

问题:

I'm using Dojo 1.9. It happens that the onSearch event runs twice instead of once in Safari and Chrome. In Firefox it runs OK.

SearchBox.onSearch = function(){
    console.log("it ran");
}

I need it to run once. How can I manage to do that?

This jsfiddle reproduce this issue.

回答1:

I think you are using the SearchBox in an unsupported case, that is without specifying its "store" property nor its "list" property.

I've put here: http://jsfiddle.net/adrian_vasiliu/g4yLQ/2/ a modified variant of your code. By setting the store property (here, to an empty dojo/store/Memory):

var store = new Memory(); // empty store
var sb = new SearchBox({store: store, ...});

I get onSearch() called only once (tested in Chrome32/Win7). Since in practice SearchBox is supposed to be used with a store, I don't think this is really a bug.



回答2:

Such uncanny behaviour seems like a Dojo bug, to me. You can always try a workaround, something like this:

require(["dojox/mobile/SearchBox", "dojo/dom-construct"],
    function (SearchBox, domConstruct) {
        var sb = new SearchBox(
                {
                    placeHolder: "search",
                    incremental: false
                },
                domConstruct.create("input", { type: "search" },
                "searchDiv")
            );

        sb.startup();

        sb.onSearch = function () {
            // "Remove" the onSearch callback, don't forget to add it
            // once again, before searching.
            this.onSearch = function () {};

            alert("ran");
        };
    }
);