首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Backbone View事件未触发

Backbone View事件未触发
EN

Stack Overflow用户
提问于 2011-06-18 08:53:03
回答 1查看 19.4K关注 0票数 19

我使用Rails3作为我的后端,而Jammit作为我的资产...现在我试着不压缩甚至打包asstes。

这个简单的事件不会运行,但是alert('asd') int inialize可以正常工作。

我已经在其他对象中尝试过其他类型的事件,但它不起作用。

我的代码如下:

代码语言:javascript
复制
var InvoiceStock = Backbone.View.extend({
  initialize: function(args) {
    _.bindAll(this, 'find_product', 'product_added');

    this.products = new ProductCollection();

    this.products.bind('add', this.product_added);

    alert('asd');
  },

  events: {
    "keypress #product_finder": "find_product"
  },

  find_product: function(e) {
    alert('teste');
  },

  product_added: function(product) {
    alert('pqp4');
  }
});

和我的RUBY HTML:

代码语言:javascript
复制
 <%= text_field 'search', :isbn_or_isbn10_or_publisher_or_authors_or_name_like, :id => 'product_finder' %> ou
 <%= link_to 'criar um produto', '', :id => 'new_product_link' %>.

这将生成以下内容:

代码语言:javascript
复制
<label>Adicionar</label>
<input id="product_finder" class="ui-autocomplete-input" type="text" size="30" name="search[isbn_or_isbn10_or_publisher_or_authors_or_name_like]" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true">
ou
<a id="new_product_link" href="">criar um produto</a>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-18 09:01:41

主干视图发生在元素的上下文中。为了使此代码正常工作,您必须在构造时将该元素分配给View,如下所示:

代码语言:javascript
复制
var InvoiceStock = Backbone.View.extend({
    el: $('#product_finder').parent()

...

或者将包含产品查找器的特定DOM对象分配给el。另一种方法是动态生成元素,并使用jQuery将其附加到DOM。我两个都用过了。

Backbone使用jQuery委托来指定其事件并将其设置为上下文。如果您没有为整个视图指定父元素,Backbone就不会正确地分配事件管理器。

票数 56
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6393174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档