Is there any specific reason why most of all implement editing in the form of the shown "display" div and the hidden "editing" div, which turn on and off when someone clicks on the corresponding "edit" button?
<div id="title"> <div class="display"> <h1> My Title </h1> </div> <div class="edit"> <input type="text" value="My Title" /> <span class="save_edit_button"></span> <a href="#" class="cancel_edit">Cancel</a> </div> </div>
Wherever I look, I see that in-place editing is basically handled this way. This approach, of course, makes sense when you view all types on the server side and deliver them to the client. However, with clean AJAX applications and frameworks like backbone.js, it seems like we could make our code much DRY by visualizing the edit form elements in place if necessary, perhaps even creating a factory method that determines which form elements to render. . eg.
- element H1 with class "title" is replaced by
<input type="text" /> - the range with the class "year_founded" is replaced by
<input type="number" min="1900" max="2050" /> - a range with the class "price" is replaced with an input with the corresponding mask, so that only enter prices.
Is this practice rendering all the elements of an in-place edit form a historical legacy left after the pages were displayed on the server side?
Given the flexibility and power we have with client MVC infrastructures such as Backbone.js, is there any reason not to create and insert form elements on the fly if necessary using the factory method? Something like that:
HTML
<div id="description"> Lorem ipsum dolar set amit... </div> <span class="edit_button"></span>
Backbone.js View
events: { "click .edit_button": "renderEditInPlaceForm", }, renderEditInPlaceForm: function:(e) { var el = $(e.currentTarget).previous(); var id = el.attr('id'); var value = el.text(); var tagName = el.tagName(); var view = new editInPlaceForm({ id: id, type: tagName, value: value }); $("#id").html(view.render().el) },
Where editInPlaceForm is a factory that returns the corresponding type of the edit form element in place based on tagName. This kind of factory also manages all its own logic to save editing, cancel editing, submit requests to the server and retransmit the corresponding source element that has been replaced with the .html () function?
It seems to me that if we use this approach, we could also make the <span class="edit_button"></span> "on the fly" buttons based on user rights, for example:
<h1 id="title"> <%= document.get("title") %> </h1> <% if (user.allowedToEdit( document, title )) { %> <span class="edit_glyph"></span> <% } %>
where the allowedToEdit function in the user model takes the model and attribute as arguments.