In my web application, a user will click a button which will call a function that triggers an AJAX call. That function accepts a single argument. I would like to be able to put that function in place of the anonymous function in the jQuery event handler. I suspect that this is a scoping issue...
Here is my working code:
(function () {
'use strict';
var postId;
function userAddFlagToPost (userId) {
return $.ajax({
type: 'POST',
timeout: 1000 * 3;,
url: '/someApi/postId',
data: {
userId: userId
},
}).done(onFlagSubmitDone)
.fail(onFlagSubmitDone);
}
function onLoggedIn() {
var userId = getCookie('some_user_auth') || null;
$(SELECTORS.POST_FLAG).on('click', function() {
postId = $(this).data('postId');
});
$(SELECTORS.SUBMIT_BUTTON).on('click', function() {
userAddFlagToPost(userId);
});
}
function init() {
eventQ.register(onLoggedIn);
}
init();
})();
I would like to be able to do:
$(SELECTORS.SUBMIT_BUTTON).on('click', userAddFlagToPost(userId));
I'd appreciate any info or advice on how to go about refactoring this code, thanks!