top | item 22877097

(no title)

conorgdaly | 5 years ago

> You could use AJAX to tell the server to add the new item, but now you need to update your UI list accordingly. You could use jQuery to construct a new DOM element and append it, but now that list element exists in two different places: in your jQuery code and in your template on the server.

Your server can just return html which is ready to be inserted into DOM. You don't need to duplicate the node view creation logic client side.

discuss

order

postpawl|5 years ago

That can get pretty complicated for pages with a lot of dynamic elements. For example, if you submit a form and have to re-render the form with an error. All the hidden “ready to be inserted into the DOM” elements also need server side code to re-render things the same way. For pages with a lot of dynamic stuff, it might be better to just build it on the client side and use an API with server side validation on the server side.

conorgdaly|5 years ago

> All the hidden “ready to be inserted into the DOM” elements also need server side code to re-render things the same way.

There seems to be some confusion. With your list example, you still make an ajax call to create a new ToDo, the server returns html which is inserted into DOM.

You'll be making an ajax call anyway, except instead of just 200 reponse/json , you'll get html. This is server side rendered. There is no """hidden “ready to be inserted into the DOM” elements"""

Yes, forms are something which, depending on complexity, can be better served with SPA type solution.

If it's not complex, I'd still keep rendering server side and just add small bit of JS logic to update form header to add/remove to error list and just replace form input field with server response.