Skip to content

Commit

Permalink
support event properties passed to trigger() or $.Event()
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Oct 3, 2012
1 parent 1241765 commit 8d2c1c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
}

$.fn.trigger = function(event, data){
if (typeof event == 'string') event = $.Event(event)
if (typeof event == 'string' || $.isPlainObject(event)) event = $.Event(event)
fix(event)
event.data = data
return this.each(function(){
Expand Down Expand Up @@ -219,6 +219,7 @@
})

$.Event = function(type, props) {
if (typeof type != 'string') props = type, type = props.type
var event = document.createEvent(specialEvents[type] || 'Events'), bubbles = true
if (props) for (var name in props) (name == 'bubbles') ? (bubbles = !!props[name]) : (event[name] = props[name])
event.initEvent(type, bubbles, true, null, null, null, null, null, null, null, null, null, null, null, null)
Expand Down
18 changes: 18 additions & 0 deletions test/zepto.html
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,24 @@ <h1>Zepto DOM unit tests</h1>

var e4 = $.Event('custom', {bubbles: false})
t.assertFalse(e4.bubbles)

var e5 = $.Event({ type: 'keyup', keyCode: 40 })
t.assertEqual('keyup', e5.type)
t.assertEqual(40, e5.keyCode)
},

testTriggerObject: function(t){
var el = $('#some_element'),
eventType, eventCode

el.on('keyup', function(e){
eventType = e.type
eventCode = e.keyCode
})
el.trigger({ type: 'keyup', keyCode: 40 })

t.assertEqual('keyup', eventType)
t.assertEqual(40, eventCode)
},

testTriggerEventObject: function(t){
Expand Down

0 comments on commit 8d2c1c4

Please sign in to comment.