在Dojo 1.7+中传递事件参数

5
我希望在dojo 1.7+中向事件传递参数。假设我有以下模块:
define(
        [ "dojo/dom", "dojo/dom-style", "dijit/registry", "js/busyIndicator",
          "dojox/mobile/ListItem", "dojo/dom-class",
          "ppwidgets/MyCommunitesRecentListItem", "js/utils" 
        ],
        function(dom, domStyle, registry, busyIndicator, ListItem, 
                 domClass, MyCommunitesRecentListItem, utils) {

            return {
                sortBy: function(sortType) {

            }};
        }
);

<h3>In html:</h3>

在普通的html中,我可以这样做:onClick="sortBy('date')"
但在dojo中,我必须使用以下代码:
data-dojo-attach-event="onClick: _sortBy"
我想要将函数sortBy()绑定到一个按钮上,如何向sortBy()函数传递sortType参数。
提前感谢您的帮助 :)
2个回答

0

Dojo提供了一个名为partial()的函数。在这里这里查看文档和教程。

基本上它允许永久地附加参数到函数中。 因此,您的代码将类似于以下内容。

define(
        [ "dojo/dom", "dojo/dom-style", "dijit/registry", "js/busyIndicator",
          "dojox/mobile/ListItem", "dojo/dom-class",
          "ppwidgets/MyCommunitesRecentListItem", "js/utils" ,
          "dojo/_base/lang" // need to add this module.
        ],
        function(dom, domStyle, registry, busyIndicator, ListItem, 
                 domClass, MyCommunitesRecentListItem, utils, lang) {

            var myFunc = function( sortType ) {
            };
            var partialFunc = lang.partial (myFunc, sortType );

            return {
                sortBy: partialFunc 
            };
        }
);

4
感谢回复。我希望你可以进行程序化操作,但如何通过HTML传递参数呢?例如:input type="button" data-dojo-attach-event="onClick: _sortBy('date')"。 - Elbassel

0

您可以将参数sortType作为相应HTML元素的属性保留,并在函数中通过event.target.sortType访问它。这是我目前使用的方法。

如果您已经找到了一种使用data-dojo-attach-event传递参数给函数的方法,请添加您的答案。:)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接