简体   繁体   English

骨干.js中的多次单击事件

[英]Multiple click events in backbone.js

var $ = jQuery = require('jquery'),
    Backbone = require('backbone'),
    Handlebars = require('handlebars'),
    _ = require('underscore'),
    Filter = require('../../libs/filters'),
    orderActionTemplate = require("../../templates/order/OrderAction.html"),
    orderListView = require('./OrderListView');

var OrderActionView = Backbone.View.extend({

    el: "#id-order-action",

    initialize: function (options) {
        var self = this;
        this.action = this.$el.find(".nav-pills li.active a").attr("action");
        if (options !== null && (typeof options !== 'undefined')) {

            self.channel_id = options.channel_id;
            self.channel_type = options.channel_type;
            this.getActions(self.channel_type, self.action); // By Default show to pack orders
            this.orderListView = new orderListView({
                action: self.action,
                channel_id: self.channel_id,
                el: ".id-to-pack"
            });
        } else {
            this.orderListView = new orderListView({
                action: self.action,
                el: ".id-to-pack"
            });

        }
        return this.orderListView;
    },

    events: {
        "click a.a-action": "getActiveTabActions"
    },

    getActiveTabActions: function (e) {
        var self = this;
        e.preventDefault();
        var liTab = $(e.currentTarget).attr("action");
        this.getActions(this.channel_type, liTab);
        console.log(self.channel_id);

        if (typeof self.channel_id !== 'undefined') {
            this.orderListView = new orderListView({
                action: liTab,
                channel_id: self.channel_id,
                channel_type: self.channel_type,
                el: ".id-to-pack"
            });
        }

    },

    getActions: function (channel_type, tab) {
        if (typeof channel_type === 'undefined') channel_type = "DEFAULT";

        if ((typeof tab !== 'undefined')) {
            var actions = Filter.actions[channel_type][tab]();
            this.$el.find("#" + tab + "-action").html(actions);
        }

    },

    render: function () {
        // this.$('.id-to-pack').empty().off();
        this.$el.html(orderActionTemplate);
        // this.orderListView.setElement(this.$('.id-to-pack')).delegateEvents().render();

    }
});

After I changed the options.channel_id in my code, the click handler is getting fired twice. 在代码中更改options.channel_id之后,点击处理程序将被触发两次。 I think there are two views in my class id-to-pack . 我认为我的班级id-to-pack有两个视图。 How do I remove previous views in the concerned element? 如何删除相关元素中的先前视图?

I tried empty() and stopListening nothing worked. 我试过empty()stopListening没有任何作用。

I think you need .remove() Removes a view and its el from the DOM, and calls stopListening to remove any bound events that the view has listenTo'd. 我认为您需要.remove() 从DOM中删除视图及其el,并调用stopListening来删除视图具有listenTo'd的所有绑定事件。

http://backbonejs.org/#View-remove http://backbonejs.org/#View-remove

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

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