简体   繁体   English

对象数组中的AddEventListener

[英]AddEventListener from object array

I have created an object which includes many action for nested Sortable, to order or modify my categories. 我创建了一个对象,该对象包括许多用于嵌套Sortable的操作,以对我的类别进行排序或修改。 Currently the object is like this. 当前对象是这样的。

var CategoriesIndexView = {
options: {
    baseIndent: 10,
    defaultIndent: 38,
    etc...
},
data: {},
events: {
    "click button.collapsible-trigger": "toggleNestedRow",
    "change #bulk-action-menu": "performBulkAction",
    "click a.category-visible": "toggleCategoryVisibility",
    "click #IndexDeleteButton": "bulkDelete"
},
initialize: function() {
    this.bindNestedSortable()
},
bindNestedSortable: function() {
    var a = this,
        b = null;
    $(".SortableList").nestedSortable(options);
},
toggleNestedRow: function(a){
    var b = $(a.currentTarget),
        c = $("i.la", b),
        d = !c.hasClass("la-plus");
    d ? this.expandCategory(b) : this.collapseCategory(b)
},
expandCategory: function(a) {},
...etc

I want to addEventListeners, based on the "events" array, i saw that on BackView.exteds method, but i am using simple javascript. 我想基于“事件”数组添加事件监听器,我在BackView.exteds方法上看到了,但是我使用的是简单的javascript。 Can i add the EventListeners based on "events", inside CategoriesIndexView? 我可以在CategoriesIndexView内基于“事件”添加EventListener吗? It is important for the Listeners to be inside the object because all other functions use "this" of the object. 对于侦听器来说,将侦听器置于对象内部非常重要,因为所有其他功能都使用对象的“ this”。

I didn't us the event property with values, but on initialisation i used jQuery .on and captured the event to return event.currentTarget. 我没有使用带有值的事件属性,但是在初始化时,我使用jQuery .on并捕获了事件以返回event.currentTarget。

like this 像这样

$("button.collapsible-trigger").on("click", function(event){
  a.toggleNestedRow( event );
});

works fine 工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM