- Joined
- Jan 18, 2012
- Messages
- 335
- Reaction score
- 119
Got all fancy-dancy with the motto update box. Incorporated the first AJAX API, going to do more for other hotel functions. The waiting time in this animation is fabricated (wanted to show off the CSS3 spinner!), usually it will be much faster.
Eventually, I'll have to write some JS helper functions. Not the best at JavaScript
Code:
@cave.route('/api/update_motto', methods=['POST'])
def api_update_motto():
if not g.user.is_authenticated():
return abort(403)
new_motto = request.form['new_motto']
if not new_motto:
return jsonify({'error': 'Please choose a new motto.'}), 400
if len(new_motto) > cave.config['MAX_MOTTO_LENGTH']:
return jsonify({'error': 'Your new motto is too long.'}), 400
g.user.motto = new_motto
db.session.commit()
return jsonify({'message': 'Your motto was set successfully.'})
Code:
$('#sb-motto-btn').click(function(){
$('#sb-motto-btn').html('<div id="bowlG"><div id="bowl_ringG"><div class="ball_holderG"><div class="ballG">');
$('#sb-motto-btn').attr('disabled', 'disabled');
$('#me-mottobox').attr('disabled', 'disabled');
$.post('api/update_motto', {'new_motto': $('#me-mottobox').val()})
.done(function(data){
$('#notif-area').prepend('<div class="alert alert-success"> \
<button type="button" class="close" data-dismiss="alert">×</button>' + data['message'] + '</div>')
})
.fail(function(xhr, textStatus, errorThrown){
$('#notif-area').prepend('<div class="alert alert-error"> \
<button type="button" class="close" data-dismiss="alert">×</button>' + JSON.parse(xhr.responseText)['error'] + '</div>')
})
.always(function(data){
$('#sb-motto-btn').html('<i class="icon-pencil icon-white"></i>');
$('#sb-motto-btn').removeAttr('disabled');
$('#me-mottobox').removeAttr('disabled');}
)});
Eventually, I'll have to write some JS helper functions. Not the best at JavaScript

