如何找到相应的点击产品型号的标识? [英] How to find the Id of model corresponding to clicked item?
问题描述
如何知道项目的ID我点击?我的code为如下:
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?
推荐答案
其实你需要的是这样的:
actually what you need is this:
-
您已经设置了html元素上的数据-ID模板
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);
}
备注我注意到您的code有太多的关闭标记,这样就不会在这里跑,
而我也在你editPost注意到您尝试_this但你永远不声明_this。
你应该有 VAR _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)获得;
我相信你的整个code例如从上面,从他们使用集合视图,因为这两个渲染另一个例子复制和使用数据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.
这篇关于如何找到相应的点击产品型号的标识?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!