XF 2.0 Inline edit with formAction

Myke623

Well-known member
I'm trying to get a handle on inline editing with basic formAction rather than creating Services. The approach I'm using is similar to Profile Posts, so there's a basic form followed by a feed of messages. Inserting messages via ajax works fine, and messages can be edited inline. The issue I'm having is that after saving an inline edit, the reply being returned (via ajax) is showing up empty rather than the view I've specified. The actual edit and save is successful though, verified with a page refresh.

When inspecting the html after the inline save, the following empty div is rendered:

HTML:
<div class="message-cell message-cell--main is-editing"></div>

The is-editing class gets inserted during the inline edit, and seems to be stuck there after the save.

My relevant controller code is as follows:

PHP:
if ($this->isPost())
{
    $input = $this->filter('note', 'array');
   
    $form = $this->formAction();
    $form->basicEntitySave($note, $input);
    $form->run();
   
    if ($this->filter('_xfWithData', 'bool') && $this->filter('_xfInlineEdit', 'bool'))
    {
        $viewParams = [
            'note' => $note,
           
            'noInlineMod' => $noInlineMod
        ];
        //var_dump($note);
        $reply = $this->view('Demo\Scratchpad:EditNewNote', 'demo_scratchpad_edit_new', $viewParams);
        $reply->setJsonParam('message', \XF::phrase('your_changes_have_been_saved'));
        return $reply;
    }
    else
    {
        return $this->redirect($this->buildLink('scratchpad'));
    }
}

I'm not sure why my view (demo_scratchpad_edit_new) isn't being returned at all. Any ideas?
 
Just to add, I'm using the standard xf/comment.js 'quick-edit' for the inline editing, and after some debugging found that my data.html is empty when XF.setupHtmlInsert is called within editSubmit:

JavaScript:
XF.setupHtmlInsert(data.html, function($html, container, onComplete)

Any pointers as to how data.html gets populated?
 
While I don't have an answer to your question(s), the way you're doing this makes it impossible for addons to extend. You should strongly consider separating your formAction into a separate function, like so:

PHP:
if ($this->isPost())
{
    $this->noteSaveProcess($note)->run();
  
    if ($this->filter('_xfWithData', 'bool') && $this->filter('_xfInlineEdit', 'bool'))
    {
        $viewParams = [
            'note' => $note,
          
            'noInlineMod' => $noInlineMod
        ];
        //var_dump($note);
        $reply = $this->view('Demo\Scratchpad:EditNewNote', 'demo_scratchpad_edit_new', $viewParams);
        $reply->setJsonParam('message', \XF::phrase('your_changes_have_been_saved'));
        return $reply;
    }
    else
    {
        return $this->redirect($this->buildLink('scratchpad'));
    }
}

PHP:
    protected function noteSaveProcess(\Your\Namespace\Entity\Note $note)
    {
        $form = $this->formAction();
        
        $input = $this->filter('note', 'array');

       $form->basicEntitySave($note, $input);

       return $form;
   }


Fillip
 
This addon was never intended to be released and is purely for my learning, but nonetheless, I appreciate the best-practice advice.

To be honest, I'm not entirely sure why having the formAction within the controller action makes it "impossible ... to extend" so if you don't mind, could you elaborate?
 
Finally figured out my problem and, embarrassingly, it was due to poorly constructed xf:foreach tags in my template macros.

So now I have a functional Scratchpad addon for xf2, following in the same vein as the Scratchpad - Demonstration AJAX Addon by @Kier (so, so long ago), but with the added feature of inline edit, leveraging existing xf2 JavaScript.
 
This addon was never intended to be released and is purely for my learning, but nonetheless, I appreciate the best-practice advice.

To be honest, I'm not entirely sure why having the formAction within the controller action makes it "impossible ... to extend" so if you don't mind, could you elaborate?
If I wanted to extend an addon of yours, and add a new field to be saved alongside your built-in fields, then I need to extend the save process. Using your original code, I would have to inspect the return value of your actionSave method (or whatever you called it) and see if it was a Redirect - if so, I would need to essentially run the entire save process again.

Using a noteSaveProcess function like that, I can simply extend that function. Saving a new field would be 5 lines of code vs 50 lines.

Does that make sense? :)


Fillip
 
Top Bottom