dispatchEvent method invocation fails with error ‘java script invalid object’

Published February 18, 2014 9:41 am

This was kind of obscure error in a WinJS application yesterday. The object is non-null, mixed with WinJS.UI.DOMEventMixin but still obj.dispatchEvent(...) fails with the error ‘java script invalid object’. After little bit of pondering, It turned out to be an issue with stubbing done by binding. This object was being set to dataSource property of a custom control. This property is an observable property of the control. The stubbing for making the property observable was causing the error.

The solution was to unwrap the object before invoking the method.

    var obj = WinJS.Binding.unwrap(obj);
    obj.dispatchEvent('myevent', { message: 'my message' });