[英]Backbone View events not firing
我使用Rails3作為我的后端,Jammit使用資產......現在我試圖不壓縮甚至打包這些。
簡單事件不會運行,但警報('asd')int inialize正在按預期工作。
我已經在其他對象中嘗試了其他類型的事件,但它沒有用。
我的代碼如下:
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:
<%= 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' %>.
產生這個:
<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>
骨干視圖發生在元素的上下文中。 為了使此代碼有效,您必須在構造時將該元素分配給View,如下所示:
var InvoiceStock = Backbone.View.extend({
el: $('#product_finder').parent()
...
或者為el分配包含產品查找器的特定DOM對象。 作為替代方案,您可以動態生成元素並使用jQuery將其附加到DOM。 我用過這兩個。
Backbone使用jQuery委托來指定和上下文化事件。 如果您沒有為整個視圖指定父元素,Backbone不會正確分配事件管理器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.