Object 在主干中编辑对象

Object 在主干中编辑对象,object,backbone.js,parse-platform,Object,Backbone.js,Parse Platform,我不熟悉parse.com环境中的主干网。我只是想编辑第二个模型对象,但我不知道如何打开第二个对象的编辑框 当前的工作模型如下,我添加了“dblclick label.todo job”:“edit1”,可以通过双击它来启动它 events: { "click .toggle" : "toggleDone", "dblclick label.todo-content" : "edit", "dblclick label.todo-jo

我不熟悉parse.com环境中的主干网。我只是想编辑第二个模型对象,但我不知道如何打开第二个对象的编辑框

当前的工作模型如下,我添加了“dblclick label.todo job”:“edit1”,可以通过双击它来启动它

events: {
      "click .toggle"              : "toggleDone",
      "dblclick label.todo-content" : "edit",
      "dblclick label.todo-job" : "edit1",
      "click .todo-destroy"   : "clear",
      "keypress .edit"      : "updateOnEnter",
      "blur .edit"          : "close"
    },
以下是允许编辑我的对象的函数

edit1: function() {
  $(this.el).addClass("editing");
  this.input.focus();
},
但是,它仅打开此对象“label.todo content”进行编辑,而我要编辑“label.todo job”。如何将焦点更改为新对象

如果你需要的话,这就是全部代码

  // The DOM element for a todo item...
  var TodoView = Parse.View.extend({

    //... is a list tag.
    tagName:  "li",

    // Cache the template function for a single item.
    template: _.template($('#item-template').html()),

    // The DOM events specific to an item.
    events: {
      "click .toggle"              : "toggleDone",
       "dblclick label.todo-content" : "edit",
      "dblclick label.todo-job" : "edit1",
      "dblclick label.todo-phone" : "edit2",
      "dblclick label.todo-email" : "edit3",
      "dblclick label.todo-website" : "edit4",
      "dblclick label.todo-address" : "edit5", 
      "click .todo-destroy"   : "clear",
      "keypress .edit"      : "updateOnEnter",
      "blur .edit"          : "close"
    },

    // The TodoView listens for changes to its model, re-rendering. Since there's
    // a one-to-one correspondence between a Todo and a TodoView in this
    // app, we set a direct reference on the model for convenience.
    initialize: function() {
      _.bindAll(this, 'render', 'close', 'remove');
      this.model.bind('change', this.render);
      this.model.bind('destroy', this.remove);
    },

    // Re-render the contents of the todo item.
    render: function() {
      $(this.el).html(this.template(this.model.toJSON()));
      this.input = this.$('.edit');
      return this;
    },

    // Toggle the `"done"` state of the model.
    toggleDone: function() {
      this.model.toggle();
    },

    // Switch this view into `"editing"` mode, displaying the input field.
    edit: function() {
      $(this.el).addClass("editing");
      this.input.focus();
    },
        edit1: function() {
      $(this.el).addClass("editing");
      this.input.focus();
    },
        edit2: function() {
      $(this.el).addClass("editing");
      this.input.focus();
    },
        edit3: function() {
      $(this.el).addClass("editing");
      this.input.focus();
    },
        edit4: function() {
      $(this.el).addClass("editing");
      this.input.focus();
    },
        edit5: function() {
      $(this.el).addClass("editing");
      this.input.focus();
    },

    // Close the `"editing"` mode, saving changes to the todo.
    close: function() {
      this.model.save({content: this.input.val()});
      $(this.el).removeClass("editing");
    },

    // If you hit `enter`, we're through editing the item.
    updateOnEnter: function(e) {
      if (e.keyCode == 13) this.close();
    },

    // Remove the item, destroy the model.
    clear: function() {
      this.model.destroy();
    }

  });
下面是在HTML中添加的对象

<script type="text/template" id="item-template">
      <li class="<%= done ? 'completed' : '' %>">
  <div class="view">
    <li><label class="todo-content"><%= _.escape(content) %></label></li>
    <li><label class="todo-job"><%= _.escape(job) %></label></li>
    <li><label class="todo-phone"><%= _.escape(phone) %></label></li>
    <li><label class="todo-email"><%= _.escape(email) %></label></li>
    <li><label class="todo-website"><%= _.escape(web) %></label></li>
    <li><label class="todo-address"><%= _.escape(address) %></label></li>
    <li><label class="todo-postcode"><%= _.escape(postcode) %></label></li>
    <button class="todo-destroy"></button>
  </div>
  <input class="edit" value="<%= _.escape(content) %>">
  <input class="edit" value="<%= _.escape(content) %>"> /*I need to edit this instead of the object above this*/
      </li>
  </script>

  • /*我需要编辑这个,而不是上面的对象*/
    事件在最深的元素上触发。这意味着
    此事件处理程序函数的
    不是您为事件侦听器选择的元素,而是实际事件发生的元素。 我不知道
    parse.com
    但是,我假设
    label.todo内容
    label.todo作业中。这使得事件处理程序的回调
    this
    变成
    label.todo content

    所以,若您显式地选择要聚焦的元素,它应该可以工作。 仅供参考,主干视图具有
    $
    ()和
    $el
    ($el)参数,用于对视图一侧的元素使用jQuery方法。由于全局
    $
    能够编辑每个控制器视图区域上的任何元素,因此始终建议使用
    此选项。$

    edit1: function() {
      this.$el.addClass("editing");
      this.$("label.todo-job").focus();
    },
    
    已编辑 我明白你的意思了。 我不知道您是如何编写HTML代码的,但是如果您的输入标记具有类名,那么您提供的代码将首先指向
    input

    edit1: function() {
      this.$el.addClass("editing");
      this.$(".yourClassNameForInput").focus();
    },
    
    或者,如果您知道您有class/id名称,也可以这样做

    edit1: function() {
      this.$el.addClass("editing");
      this.$("input").eq(0).focus();
    },
    ....
    edit5: function() {
      this.$el.addClass("editing");
      this.$("label.todo-job").eq(4).focus();
    }
    

    谢谢Suish,但这段代码仍然为编辑ie todo内容带来了相同的元素。“label.todo job”不在“label.todo job”内,但与“label.todo content”处于同一级别,如果我正确理解您的意思。请您提供更多代码并准确说明您想要实现的目标,好吗?如果可以的话,jsfiddle是最好的。只是在问题的底部添加了我的全部代码。代码允许编辑第一个项目,而我想通过双击编辑所有项目。目前,双击任何项目只会显示第一个要编辑的元素。Suish,它实际上并没有指向第一个对象,我可能不理解使用类和id时的含义。但是当我更改下面的第一个和第二个列表项目时,仍然只会显示todo内容进行编辑。我在下面的问题中添加了我的HTML代码。Suish,我现在遇到了问题,正如您在我的HTML代码中看到的,我添加了一个输入对象,它是在我的主干模板中传递编辑类时拾取的。现在我需要告诉代码拾取第二个输入对象,而不是第一个。