backbone页面重绘事件监听不到解决方案 | Jquery Ajax动态加载模版 backbone监听不到事件

来源:互联网 发布:linux truncate函数 编辑:程序博客网 时间:2024/06/04 19:28

// 下面代码是用typescript 写的 js的超集
export class LoginView extends Backbone.View {    templateURL: string = 'modules/login/loginView.html';    template: any;    constructor(options?: any) {        super(options);    }    initialize() {        _.bindAll(this, 'render', 'login');        this.bind('getTemplateDone', this.render);    }    events= {        'click input#btn_login': 'login'    }    login() {        alert('adsf');    }    init() {        this.getTemplate();    }    render() {        $(this.el).append(this.template());        this.trigger('renderComplete:login', this, 'test');        this.delegateEvents();        return this;    }    getTemplate() {        var _this = this;        $.ajax({ url: this.templateURL })            .done(function (data) { _this.template = _.template(data); _this.trigger('getTemplateDone'); } )            .fail(function () { alert("error"); } );    }}


重绘后页面上的button不是原来的button了,只是id相同但是两个对象,事件没了。

重新绘制页面元素后原有的事件需要重新触发绑定.可在render之后使用this.delegateEvents();