how to add in-line editing of model attributes,
for example, when I have a Player model
var Player = Backbone.Model.extend({ defaults: { id: 0, name: '', points: 0 }, initialize: function(){
and a playerRow view
var PlayerRow = Backbone.View.extend({ tagName: 'li', className: 'player', events: { 'click span.score': 'score', 'blur input.name': 'rename', 'click div.playername': 'renderRename' }, initialize: function(){ _.bindAll(this, 'render', 'rename'); this.model.bind('change', this.render); }, render: function(){ var template = Tmpl.render("playerRow", { model : this.model }); $(this.el).html(template); return this; }, renderRename: function() {
and my playerRow template
<script type="text/template" id="playerRow-template"> <% if ( model.get('name') == '' ) { %> <div class="state-edit"><input type="text" class="name" name="name"></input></div> <% } else { %> <div class="playername"><%= model.get('name') %></div> <% } %> </script>
or I set a specific property of my model that contains the state (by default or editing) that triggers re-rendering and in my template instead of testing if name == '' I can check this state property.
or am I doing this inline, as I say in my comment on renderRename, I just change the DOM to the input field, but I think this is a bad way to do this. Will this cause problems with already related events? e.g. 'blur input.name': 'rename'
I doubt that creating a new view for editing is the best way because I want inline editing only for this name field, I donβt want all other player templates to be duplicated in the player template and the editplayer template.
so, bottom line, my question is: what is the best way to handle inline editing
- separate presentation
- inline just adds an input field (I don't know if this works well with related events)
- when my model contains a state variable for this attribute (it will work, but it sounds strange to have data related to the model view).