Elbassel
Elbassel

Reputation: 454

Passing arguement to an event in dojo 1.7+


I want to pass an argument to an event in dojo 1,7+.
suppose I have the following module:

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>

In normal html I can do that: onClick="sortBy('date')"
but in dojo, I have to use this:
data-dojo-attach-event="onClick: _sortBy"
I want to bind the function sortBy( ) to a button, How can I pass the sortType to sortBy( ) function.
Thanks in advance :)

Upvotes: 5

Views: 1315

Answers (2)

Himanshu
Himanshu

Reputation: 1002

You can keep the argument sortType as an attribute in the corresponding HTML element and can access it by event.target.sortType in the function. This is the way that I am using currently

If you have found a way to pass parameters to function using data-dojo-attach-event then please add your answer. :)

Upvotes: 0

frank
frank

Reputation: 3330

Dojo provides a function called partial(). Look here and here for documentation and tutorial.

Basically what it allows is to attach arguments permanently to the function. so your code would look something like this.

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 
            };
        }
);

Upvotes: 0

Related Questions