Skip to content

Instantly share code, notes, and snippets.

@ahawkins
Created April 7, 2010 00:03
Show Gist options
  • Save ahawkins/358293 to your computer and use it in GitHub Desktop.
Save ahawkins/358293 to your computer and use it in GitHub Desktop.
// duplicates the current functionality
settingsCheck({
askUrl: '/settings/todo/ready',
formUrl: '/settings/todo/edit',
selector: 'a.new-todo',
event: 'click',
defaultAction: function() {
$("a.new-todo").live('click', function(){
$("#" + $(this).attr('rel')).toggle('fast');
return false;
});
},
clear: function() {
$("a.new-todo").die('click');
}
});
//Where the magic happens
/*
"Intercepts" actions that need configuration before they can be completed.
For example, new todo links are put on the page, but the user may have to set their
time zone and phone number. So when the click on a new todo link, it should either
give them a modal box to update their information or display the form.
This method encapsulates this pattern:
1. Ask the server a boolean question where the answer should be yes
2. If yes, go about your business.
3. If No,
4. (click events should do something else)
5. Click should now open a modal box with a form
6. Load some form into the modal box
7. Ajaxify the form
8. When the user submits the form and it's a
9. Success: do what you were originally going to do in step 2
10. Invalid: display errors and wait for the user to resubmit
In the new todo context, clicking a new todo link is the event we want to intercept.
The method takes one agrument: the settings object. Here are the default settings:
invalidForm: function(form, errors) { $(form).formtasticErrors(errors) },
The here are the available settings:
askUrl: url to get. default action is applied when the server answers in 2xxx
formUrl: url of the settings update form
event: event to listen on.
selector: used to apply $(selector).live(event, function()) {}
invalidForm: function(form, errors) {} // form is a dom object for the form inside the facebox. errors is an array of erros
defaultAction: function() {} //What you want to do by default
clear: function() { } //Used to unbind the old event
A real life example:
$(function() {
settingsCheck({
askUrl: '/settings/todo/ready',
formUrl: '/settings/todo/edit',
selector: 'a.new-todo',
event: 'click',
defaultAction: function() {
$("a.new-todo").live('click', function(){
$("#" + $(this).attr('rel')).toggle('fast');
return false;
};
},
clear: function() {
$("a.new-todo").die('click');
}
});
});
*/
settingsCheck = function(options) {
var defaults = {
invalidForm: function(form, errors) { $(form).formtasticErrors(errors) },
};
var config = $.extend(defaults, options);
console.log(config);
$.ajax({
dataType: 'html',
type: 'get',
url: config.askUrl,
beforeSend: function(xhr) { console.log('checking settings...');},
complete: function(xhr, status) {
if(status != 'success') { // todo links should do something else
$(config.selector).live(config.event, function() {
$.facebox(function() {
$.get(config.formUrl, function(data) {
$.facebox(data)
$("#facebox input[type='submit']").button();
$("#facebox form").submit(function(){
var form = this;
$.ajax({
dataType: 'json',
type: $(form).attr('method'),
url: $(form).attr('action'),
data: $(form).serialize(),
complete: function(update_xhr, update_status) {
if(update_status == 'success') {
$(document).trigger('close.facebox');
config.clear();
config.defaultAction();
} else {
config.invalidForm(form, $.parseJSON(update_xhr.responseText));
}
}
});
return false;
});
});
});
return false;
});
} else { // todo links can show the todo form
config.defaultAction();
}
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment