Have you ever written code that looks like this?
If so, you quickly find out that you are writing a lot of display code in your callbacks, that its hard to reuse this code, and its hard to maintain this code.
$.ajax({
url : ...,
success : function(){
$msg = $("<p>Success saving.</p>")
.hide();
$('#messages').append(
$msg;
);
$msg.fadeIn("slow");
},
error : function(){
...
}
})
Using envelope, your callbacks become much more elegant
Envlope allows you create messages that listen for events and display a message when that event is triggered.
$.ajax({
url : ...,
success : function(){
$.trigger('save.success')
},
error : function(){
$.trigger('save.error');
}
});
Use the form below to setup an event and trigger it by clicking the "Generate Event" button
Getting started is easy:
To setup envelope, just attach it to a jQuery wrapped element and pass it some arguments.
The first, and optional, argument is the plugin options. These specify the UI framework to use and the default timeout before auto-close elements disapear.
The second argument to pass in is an array of event options. The event options define the behavior of the events to listen to.
$("#messages").envelope( { //plugin options uiFramework: 'bootstrap', autoCloseTimeout: 5000 }, [ //array of events, with options { name: 'user.save', message: "User saved.", callback : function(){}, autoClose: true, addCloseButton: true, type: 'success' } ] );
The first, and optionl, argument that is passed into the plugin are the options for the plugin itself.
The second option is an array or events that envelope should listen for. Each even has its own set of options.
You can add events to the plugin after initialization by using the add method.
$("#messages").envelope('add', { name : 'test.success', message: 'Success saving test.', type: 'success', addCloseButton : true, autoClose : true } );
You can remove events to the plugin after initialization by using the remove method.
$("#messages").envelope('remove','test.success');
To append text to a message when the event is triggered just pass the additional text as the second parameter of the trigger()
method.
$("selector").trigger("save.success","Additional text");