Last active
November 23, 2018 09:58
-
-
Save gartmeier/3dc02db0d6ae206957d2c9b6a8f2a29d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
$.fireEvent = function(name, data) { | |
var deferred = $.Deferred(), | |
callback = 'cb' + Math.random().toString(36).substring(7); | |
if (data) { | |
data.callBackFunc = callback; | |
data.callBack = callback; | |
data.source = $.fireEvent.defaults.source; | |
} | |
window[callback] = function(data) { | |
try { | |
data = JSON.parse(data); | |
} catch (e) { | |
console.error('couldn\'t parse response'); | |
} | |
deferred.resolve(data); | |
delete window[callback]; | |
}; | |
if ($.fireEvent.defaults.timeout) { | |
setTimeout(function() { | |
deferred.reject('timeout'); | |
delete window[callback]; | |
}, $.fireEvent.defaults.timeout); | |
} | |
Ti.App.fireEvent(name, data); | |
return deferred; | |
}; | |
$.fireEvent.defaults = { | |
source: 'modal', | |
timeout: 500 | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: