简体   繁体   English

骨干视图点击事件未触发

[英]Backbone view click event not firing

Here is the code: 这是代码:

var view = Backbone.View.extend({
  el:'#comment',
  template:'',
  initialize:function(){
    this._getTemplate();
    this.render();
  },
  event:{
    'click #submit_btn' : 'submit'
  },
  _getTemplate:function(){
    $.ajax({
      ...
    });
  },
  render:function(){
    this.$el.html(this.template);
  },
  submit:function(event){
    alert('click');
  }
});

I use ajax to get html template from server and it works well. 我使用ajax从服务器获取html模板,并且效果很好。 I have no problem in loading template. 我加载模板没有问题。

Here is the div which I wanna insert the template into. 这是我要插入模板的div。

<div id="comment">...</div>

Here is the template. 这是模板。 I just show the simple structure. 我只是显示简单的结构。

<div>...</div>
<button id="submit_btn">submit</button>
<div>...</div>

Can someone help me to solve it? 有人可以帮我解决吗?

It looks like the event string is set up correctly and the render function is doing the right thing. 看来事件字符串设置正确,并且render函数做正确的事。 The one issue I see is that the 'initialized' function should be 'initialize' as per the Backbone View Extend documentation. 我看到的一个问题是,根据Backbone View Extend文档,应该对“初始化”功能进行“初始化”。

Reference: http://backbonejs.org/#View-extend 参考: http : //backbonejs.org/#View-extend

The event not firing because when the event is attempted to bind to the DOM element (ie at the time of the View construction), the DOM element does not exist. 事件未触发,因为当尝试将事件绑定到DOM元素时(即,在构造View时),DOM元素不存在。 From the Backbone docs : 骨干文档

Backbone will automatically attach the event listeners at instantiation time, right before invoking initialize. 在调用初始化之前,Backbone会在实例化时自动附加事件侦听器。

Since the #submit_btn DOM element matcher returns nothing, no event is bound. 由于#submit_btn DOM元素匹配器不返回任何内容,因此不会绑定任何事件。

Your technique of fetching the template within the initialize() method will be problematic. 您在initialize()方法中获取模板的技术会出现问题。 In most OO-based development patterns, the Constructor of the object should not do a lot of work, and the returned instance should be fully initialized when the Constructor completes. 在大多数基于OO的开发模式中,对象的构造函数不应执行大量工作,并且当构造函数完成时,应完全初始化返回的实例。 Backbone Views are no different in this respect. 骨干网视图在这方面没有什么不同。 Your Constructor as written here attempts to do quite a bit of work, but there is no benefit to fetching the template as a part of the object creation here. 此处编写的构造方法会尝试做很多工作,但是将模板作为对象创建的一部分来获取并没有任何好处。 In fact there is some harm too: what if you end up creating more than one instance of your View ? 实际上也有一些危害:如果最终创建多个View实例怎么办? You would end up fetching the template more than once, even though the contents would be the exact same. 即使内容完全相同,您最终还是要多次获取模板。

Forcing an XHR of any kind to be synchronous is also going to be problematic. 强制任何一种XHR同步也是有问题的。 During a synchronous request the browser's main thread is busy waiting for the response. 在同步请求期间,浏览器的主线程正忙于等待响应。 In this example if the template ever takes longer than a few hundred milliseconds to arrive, your application will appear "frozen" to the user. 在此示例中,如果模板花费的时间超过几百毫秒,则您的应用程序将对用户显示为“冻结”。 This provides a very poor user experience. 这提供了非常差的用户体验。 Asynchronous code may feel like it's more complex, but if you are building a JS-based application of even minimal complexity you will be required to deal with it. 异步代码可能会让您觉得更复杂,但是如果您要构建一个基于JS的应用程序,则该应用程序的复杂度甚至会降至最低。 Since Javascript is (mostly) single-threaded, asynchronous execution becomes an essential tool of any application. 由于Javascript(主要是单线程),因此异步执行成为任何应用程序必不可少的工具。

A better solution is to either embed the template into the hosting HTML page content, or use a JS modularization technique (RequireJS, CommonJS, others) to effectively package the template up with the module that needs it. 更好的解决方案是将模板嵌入到托管HTML页面内容中, 或者使用JS模块化技术(RequireJS,CommonJS等)将模板与需要模板的模块有效地打包在一起。

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

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