Custom event data argument is 'undefined'

  • 1
  • Question
  • Updated 4 years ago
  • Answered
  • (Edited)
I'm trying to use custom event publishing using skuid.events.publish and skuid.events.subscribe.  The basic event system is working, but I'm having problems getting the data argument to work.  If I publish with:

skuid.events.publish('my.custom.event', 'my argument')

I get an error during the publishing:

Uncaught TypeError: Function.prototype.apply: Arguments list has wrong type

If instead I publish using a generic object for the data parameter, such as:

skuid.events.publish('my.custom.event', {myarg: 'my argument'})

the publish works fine, and the subscribe handler gets called.  However, the data argument passed to the handler is 'undefined' rather than containing my custom object.  My handler looks something like this:

skuid.events.subscribe('my.custom.event', function(data) { console.log(data); });

Any hints on what I might be doing wrong?

Thanks,

- Chris
Photo of Chris

Chris

  • 1,632 Points 1k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,004 Points 20k badge 2x thumb
Chris, 

Sorry this isn't documented, we'll add this, but you need to pass an array, like this:

skuid.events.publish('my.custom.event', ['my argument 1','my argument 2']);

Each Item in the Array will be converted into an argument handed to the subscriber function. We recommend just having one argument, and having it be a JavaScript object with as many properties as you need, but this is up to you.

For instance:


var eventParams = {
   foo: 'bar',
   name: 'Zach'
};
skuid.events.publish('my.custom.event', [eventParams, 'Mint Chocolate Chip']);

Then your subscriber function should do this:

skuid.events.subscribe('my.custom.event',function(eventParams, favoriteIceCream){
    console.log('Foo: ' + eventParams.foo);
    console.log('Name: ' + eventParams.name);
    console.log('Favorite Ice Cream: ' + favoriteIceCream);
});
Photo of Chris

Chris

  • 1,632 Points 1k badge 2x thumb
Phew - I was worried it was me!  Thanks for the writeup... I'll give it a go.

- Chris
Photo of Chris

Chris

  • 1,632 Points 1k badge 2x thumb
Just wanted to confirm that this method works great.  Thanks!
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,004 Points 20k badge 2x thumb
I have updated the skuid.events API documentation to clarify the expected data arguments.