Created
February 14, 2017 19:01
-
-
Save progsmile/99b9e059c0cc4b1b3cdd6f12d245ec26 to your computer and use it in GitHub Desktop.
Ajax Links
This file contains 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
import toastr from 'toastr' | |
// this = jQuery element .ajaxLink | |
export default { | |
redirectHome(){ | |
location.href = '/'; | |
}, | |
reload() { | |
location.reload(); | |
}, | |
removeRow(){ | |
this.parentsUntil('tbody').remove(); | |
}, | |
removeParent(){ | |
this.parent().remove(); | |
}, | |
message(response) { | |
toastr[response.status](response.message); | |
}, | |
opacited(){ | |
this.toggleClass('opacited'); | |
} | |
}; |
This file contains 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
import toastr from 'toastr'; | |
import callbacks from './ajaxLinkCallbacks' | |
$(document).on('click', '.ajaxLink', function (e) { | |
e.preventDefault(); | |
let $$ = $(this); | |
let ajaxParams = { | |
url: $$.attr('href'), | |
type: $$.data('request') || 'post', | |
success: function (response = {}) { | |
try { | |
let callbackString = $$.data('callback'); | |
if (!callbackString) { | |
console.log('no callback', response); | |
} else { | |
callbackString.split('|').map(callback => callbacks[callback].call($$, response)); | |
} | |
} catch (error) { | |
console.log(error); | |
toastr.error("Something went wrong"); | |
} | |
} | |
}; | |
let dataParams = $$.data('params'); | |
if (dataParams) { | |
ajaxParams.data = dataParams | |
.split('&') | |
.reduce((params, keyValue) => { | |
let [key, value] = keyValue.split('='); | |
params[key] = value; | |
return params; | |
}, {}); | |
} | |
$.ajax(ajaxParams); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment