如何找到被点击的item对应的model的Id? [英] How to find the Id of model corresponding to clicked item?

查看:22
本文介绍了如何找到被点击的item对应的model的Id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何知道我点击的项目的 ID?我的代码如下:

How to know the Id of item I clicked? my code is given below:

$(function() {
  ipl.mvc.view.openings_view = ipl.mvc.view.view_base.extend({
    template: '/assets/t/plmt/companies.tmpl.html',
    ID: '#tmpl_openings',
    events: {
      "click #edit": "editpost"
    },
    initialize: function() {
      var _this = this;
      _.bindAll(this, 'addOne', 'addAll', 'render', 'editpost');
      _this.loadTemplate(_this.template, function() {
        _this.model = new ipl.mvc.model.companies_model([]);
        _this.model.view = _this;
        _this.model.bind('change', _this.render, _this);

      });
    }

    ,
    render: function() {
      var _this = this
      jsonData = _this.model.toJSON();
      _.each(jsonData, function(model) {
        $(_this.ID).tmpl(model).appendTo(_this.el);
        return _this;
      });
    }
    ,
    editpost: function(e) {
      console.log("EDIT CLICKED");
      e.preventDefault();
      var ele = $(e.target);
      console.log(ele);
      _this.model = new ipl.mvc.collection.companies_collection([]);
      var id = _this.model.get("id");
      alert(id);
    }
  });
});

和模板

<a href="!/editpost/${id}" data-id="${id}"><button id="edit"  ><img   src="/assets/img/pencil.png" /></button></a>

我需要 id 才能在编辑功能中使用,但没有从模板中获取 ${id},我按照这些步骤操作却没有得到?如何从模板中获取点击项目的${id}?

I need id to use in edit function, but not getting ${id} from template, and I follow those steps and I didn't get it? how to get the ${id} of clicked item from template?

推荐答案

实际上你需要的是:

  1. 您已经在模板中的 html 元素上设置了 data-id

  1. you have set the data-id on the html element in the template

您添加了要在正确事件 (editPost) 上执行的方法

you added a method to be executed on the right event (editPost)

在那个 editPost 方法中,你可以这样做:

in that editPost method you can do this:

editPost: function(e){
  e.preventDefault();
  var id = $(e.currentTarget).data("id");
  var item = this.collection.get(id);
}

备注 我注意到你的代码有太多的结束标签,所以它不会在这里运行,我还注意到在您的 editPost 中您尝试了 _this,但您从未声明过 _this.你应该在那个方法中有 var _this = this; .

remark i noticed your code has too many closing tags, so it won't run here, and i also noticed in your editPost you try _this but you never declare _this. you should have had var _this = this; in that method.

备注 2 我不明白为什么在这个例子中你甚至需要一个数据 ID,有两种渲染视图的方法,视图绑定了一个集合,或者它有一个模型.将集合绑定到视图时,您可能需要从单击的元素中获取正确的 id 以在集合中找到正确的模型

remark 2 i don't understand why you even need a data-id in this example, there are two ways of rendering a view, either the view has a collection bound to it, or it has a model. When binding a collection to a view, you might somehow need to get the correct id from the element clicked to find the right model in your collection

但在您的示例中,您的视图绑定了一个模型,因此您只有 1 个 ID,您只需调用 this.model.get('id');我相信您上面的整个代码示例是从另一个示例中复制的,其中他们在视图中使用了一个集合,因为您的渲染和 data-id 属性的使用都表明了这一点.

but on your example, your view has a model bound to it, so you only have that 1 ID, which you can just get by calling this.model.get('id'); i believe your entire code example from above, is copied from another example where they use a collection in the view, since both your rendering, and the use of the data-id property suggests this.

这篇关于如何找到被点击的item对应的model的Id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆