【发布时间】:2016-07-22 13:46:41
【问题描述】:
我正在动态创建对象,这些对象在单击时构造视图并将事件绑定到构造的视图。但是,click 事件始终将自身附加到最后创建的对象。我确实希望每个对象都有相同的点击事件,但是我传递给新类的数据对于每个实例都是唯一的。
对象创建:
$.each(data,function(i,json)
{
var channelWrapper = new ChannelDisplayView(that.channelContainer, json);
that.channelContainer.append(channelWrapper.createView());
});
ChannelDisplayView.js
function ChannelDisplayView(parent, data){
this.parent = parent;
this.data = data;
}
ChannelDisplayView.prototype.createView = function(){
this.channelWrapper = $("<div/>").addClass("channelDisplay").attr("id", this.data.fullName);
this.channelTitle = $("<div/>").addClass("channelTitle").html(this.data.fullName);
this.channelSource = $("<div/>").addClass("sourceType").html(this.data.sourceType);
this.channelWrapper.append(this.channelTitle);
this.channelWrapper.append(this.channelSource);
that = this;
this.channelWrapper.unbind().on("click",function(event){
ControllersSingleton.getInstance().getProgramController().setupPage(that.data);
});
return this.channelWrapper;
}
如您所见,每个类都将保存自己的 JSON 数据,如果单击“div”,则应使用该数据传递给另一个控制器的实例。无论我点击哪个 div,发送的数据始终来自最后创建的对象。
我在这里遗漏了什么明显的东西吗?
【问题讨论】:
-
jsFiddle 或 jsBin 的任何机会?
标签: javascript jquery jquery-events