将争论传递给道场1.7+中的一个事件

Elb*_*sel 5 javascript dojo arguments parameter-passing


我想将一个参数传递给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>
Run Code Online (Sandbox Code Playgroud)

在普通的html中,我可以这样做:onClick ="sortBy('date')"
但是在dojo中,我必须使用它:
data-dojo-attach-event ="onClick:_sortBy"
我想绑定函数sortBy()到一个按钮,我如何将sortType传递给sortBy()函数.
提前致谢 :)

fra*_*ank 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 
            };
        }
);
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回复。这是以编程方式执行的,但是如何传递参数抛出 html 例如: input type="button" data-dojo-attach-event="onClick: _sortBy('date')" (4认同)