Last active
August 29, 2015 13:56
-
-
Save wiesson/9173218 to your computer and use it in GitHub Desktop.
XMLHttpRequest "DELETE" call to the flask backend. No jquery is required.
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
<button id="deleteDevice" class="btn btn-danger btn-xs" | |
data-link="{{ url_for('monitor.delete_device', id_device=device.id_device) }}"> | |
<span class="glyphicon glyphicon-remove-circle"></span> | |
Delete Device | |
</button> | |
<script type="text/javascript"> | |
var t = document.getElementById('deleteDevice'); | |
t.addEventListener('click', function () { | |
deleteDevice(t.dataset.link); | |
}); | |
function deleteDevice(url) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open("DELETE", url, true); | |
xhr.onload = function (e) { | |
if (xhr.readyState === 4) { | |
if (xhr.status === 200) { | |
window.location.assign('{{ url_for('monitor.index') }}'); | |
console.log(xhr.responseText); | |
} else { | |
console.error(xhr.statusText); | |
} | |
} | |
}; | |
xhr.onerror = function (e) { | |
console.error(xhr.statusText); | |
}; | |
xhr.send(null); | |
} | |
</script> |
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
# Flask backend | |
@mod.route('/<string:id_device>') | |
@mod.route('/<string:id_device>', methods=['DELETE']) | |
@login_required | |
def view_device(id_device): | |
""" | |
View a single device and delete a single device | |
""" | |
if request.method == 'DELETE': | |
Devices.query.filter_by(id_device=id_device).delete() | |
db.session.commit() | |
# if id_device not in Device.query.filter_by(id_device=id_device).first(): | |
flash('Device %s has successfully been deleted.' % id_device, 'success') | |
return "" | |
# else ... | |
return render_template(...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment