Try using a different model. I would use the ID of the element to  
carry the Here's an adaptation of what I do on a page that can have  
thousands of DIVs, each of which will be individually editable at a  
click:

<div id="editable_stuff">
<div id="div_1">
<h2 id="title_1" class="title">A lovely title</h2>
<p id="description_1" class="description">Some incredibly useful text  
here</p>
</div>
...
<div id="div_11">
<h2 id="title_11" class="title">A lovely title</h2>
<p id="description_11" class="description">Some incredibly useful text  
here</p>
</div>
</div>

//extend a few things for convenience (watch out for line-breaks below):
Object.extend(Ajax.InPlaceEditor.prototype, {
        onclickCancel: function() {
                this.onComplete();
                this.leaveEditMode();
                this.dispose();
                return false;
        },
        convertHTMLLineBreaks: function(string) {
                return string.replace(/<br[^>]*?>/gi, "\n");
        }
});
Element.addMethods({
        openEditor: function(element){
                var element = $(element);
                var parts = element.id.split(/_/);
                var editor = new Ajax.InPlaceEditor(
                        parts[1],
                        'your_server.php',
                        {
                                callback:function(form,value){
                                        return 'id=' + parts[1] +
                                        '&kind=' + parts[0] +
                                        '&value=' + encodeURIComponent(value);
                                },
                                ajaxOptions: {
                                        onComplete: function(){
                                                this.dispose();
                                        }
                                },
                                cols:20,
                                rows:10
                        }
                );
                editor.enterEditMode('click');
        }
});

//now we watch the entire container div for clicks, and edit what  
actually gets clicked

$('editable_stuff').observe('click',function(evt){
        var elm = Event.element(evt);
        if(elm.hasClassName('title') || elm.hasClassName('description')){
                elm.openEditor();
        }
});

On the server, you want to look for $_POST['id'], $_POST['kind'], and  
$_POST['value'], and use them to figure out which item you are editing  
and which field of that item.

if($item = ActiveRecord::FindById('items',$_POST['id'])){
        $field = clean($_POST['kind']);
        if(in_array($field,$array_of_legal_fields)){
                $item->$field = clean($_POST['value']);
                $item->save();
                header('Content-type: text/html; charset=utf-8');
                print $item->$field;
        }else{
                header('HTTP/1.0 403 Forbidden');
        }
}else{
        header('HTTP/1.0 404 Not found');
}

The only requirement for this to work would be that you follow a  
naming convention with your elements that matches your DB schema (or  
provide some sort of concordance in the server code).

Walter

On Feb 18, 2009, at 11:29 AM, SamuelXiao wrote:

> My question is, how can I check whether the user click the title or
> the content?  If he clicks the content, modify it, the new content
> will be sent and updated.  And if he clicks the title, modify it, the
> new title will be sent and updated.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to