Go to Google Groups Home    Low Pro
Re: Adding a delete confirmation

KJoyner <k...@kjoyner.com>

I changed the auth code as follows but cannot figure out any way to
furthur simplify.

var AJ =
{
  encode_authenticity_token:function( token )
  {
      return encodeURIComponent( $F(token) )
  },

  authenticity_token_query_parameter_for_page:function()
  {
      return 'authenticity_token=' + AJ.encode_authenticity_token(
        $$( 'input[ name = "authenticity_token" ]' ).first() )
  }

}

I also moved to event delegation instead of trying to make the reload
work. The javascript now looks as follows:

Remote.Delete = Behavior.create( Remote.Base,
{
  onclick: function( event )
  {
    var element = Event.element( event );
    if ( element.hasClassName( 'delete_link' ) )
    {
      if ( confirm( 'Are you sure?' ) )
      {
        var options =
        {
            url:        element.href.gsub( '/delete$', '' ),
            method:     'delete',
            parameters:
AJ.authenticity_token_query_parameter_for_page()
        };
        options = Object.extend( options, this.options );

        this._makeRequest( options );
      }
      return false;
    }
    return true;
  }

});

Event.addBehavior(
{
  '#item_list' : Remote.Delete

});

Notice, that this attaches a behavior to a div with an ID of
item_list. If the element that received the click has a classname of
delete_link, then a confirmation will be displayed and the element
deleted through the destroy method.

On 25 Jan, 00:25, Jarkko Laine <jar...@jlaine.net> wrote:

> On 25.1.2008, at 9.20, KJoyner wrote:

> > I have added the following that now works, except for the reload which
> > does not appear to be working as I intended. For completeness, I have
> > included my updated delete method.

> > var AJ =
> > {
> >  encode_authenticity_token:function( token )
> >  {
> >      return encodeURIComponent( $(token).value )

> Can't answer to your reload question off the top of my head, but you
> can replace $(token).value with $F(token) (seehttp://prototypejs.org/api/utility/dollar-f)

> >        document.body.select( 'input[ name = "authenticity_token" ]' )
> > [ 0 ] )

> This is also same as $$('input[name="authenticity_token']).first() (http://prototypejs.org/api/utility/dollar-dollar
> ). If you give the input field an id, it can be reduced to $
> ('authenticity_token').

> --
> Jarkko Lainehttp://jlaine.nethttp://dotherightthing.comhttp://www.railsecommerce....