Skip to content

Commit

Permalink
Make purr support opt-in via best_in_place.purr.js.
Browse files Browse the repository at this point in the history
  • Loading branch information
Empact committed Jul 25, 2012
1 parent 2a61ccd commit 034fed8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
25 changes: 22 additions & 3 deletions README.md
Expand Up @@ -303,13 +303,12 @@ thanks to Rails 3.1. Just begin including the gem in your Gemfile:

gem "best_in_place"

After that, specify the use of the jquery, jquery.purr and best in place
After that, specify the use of the jquery and best in place
javascripts in your application.js, and optionally specify jquery-ui if
you want to use jQuery UI datepickers:

//= require jquery
//= require jquery-ui
//= require jquery.purr
//= require best_in_place

If you want to use jQuery UI datepickers, you should also install and
Expand Down Expand Up @@ -340,7 +339,6 @@ After that, install and load all the javascripts from the folder
**/public/javascripts** in your layouts. They have to be in the order:

* jquery
* jquery.purr
* **best_in_place**

You can automatize this installation by doing
Expand All @@ -360,6 +358,27 @@ Finally, as for Rails 3.1, just add a binding to prepare all best in place field

---

## Notification

Sometimes your in-place updates will fail due to validation or for some other reason. In such case, you'll want to notify the user somehow. **Best in Place** supports doing so through the best_in_place:error event, and has built-in support for notification via jquery.purr, right out of the box.

To opt into the jquery.purr error notification, just add best_in_place.purr to your javascripts, as described below. If you'd like to develop your own custom form of error notification, you can use best_in_place.purr as an example to guide you.

###Rails 3.1 and higher

It's as simple as adding:

//= require best_in_place.purr

###Rails 3.0 and lower

You'll have to load the following additional javascripts, in this order, after loading jquery and **best_in_place**:

* jquery.purr
* **best_in_place.purr**

---

## Security

If the script is used with the Rails Gem no html tags will be allowed unless the sanitize option is set to true, in that case only the tags [*b i u s a strong em p h1 h2 h3 h4 h5 ul li ol hr pre span img*] will be allowed. If the script is used without the gem and with frameworks other than Rails, then you should make sure you are providing the csrf authenticity params as meta tags and you should always escape undesired html tags such as script, object and so forth.
Expand Down
7 changes: 1 addition & 6 deletions lib/assets/javascripts/best_in_place.js
Expand Up @@ -223,12 +223,7 @@ BestInPlaceEditor.prototype = {
loadErrorCallback : function(request, error) {
this.element.html(this.oldValue);

// Display all error messages from server side validation
jQuery.each(jQuery.parseJSON(request.responseText), function(index, value) {
if( typeof(value) == "object") {value = index + " " + value.toString(); }
var container = jQuery("<span class='flash-error'></span>").html(value);
container.purr();
});
this.element.trigger(jQuery.Event("best_in_place:error"), [request, error])
this.element.trigger(jQuery.Event("ajax:error"));

// Binding back after being clicked
Expand Down
10 changes: 10 additions & 0 deletions lib/assets/javascripts/best_in_place.purr.js
@@ -0,0 +1,10 @@
//= require jquery.purr

$(document).on('best_in_place:error', function(event, request, error) {
// Display all error messages from server side validation
jQuery.each(jQuery.parseJSON(request.responseText), function(index, value) {
if( typeof(value) == "object") {value = index + " " + value.toString(); }
var container = jQuery("<span class='flash-error'></span>").html(value);
container.purr();
});
});
2 changes: 1 addition & 1 deletion test_app/app/assets/javascripts/application.js
@@ -1,7 +1,7 @@
//= require jquery
//= require jquery-ui
//= require best_in_place
//= require jquery.purr
//= require best_in_place.purr
//= require_self

$(document).ready(function() {
Expand Down

0 comments on commit 034fed8

Please sign in to comment.