主干视图事件未触发
我使用 Rails3 作为我的后端,使用 Jammit 来资产...现在我尝试不压缩甚至打包资产。
简单事件没有运行,但alert('asd') int初始化正在按预期工作。
我已经在其他对象中尝试过其他类型的事件,但它不起作用。
我的代码如下:
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>
I am using Rails3 as my backend and Jammit to asset... Now I am trying to not compress and even package the assets.
The simple event don't get run, but the alert('asd') int initialize is working as expected.
I have already tried other kinds of events in other objects too, but it didn't work.
My code as follow:
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');
}
});
and my 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' %>.
which generates this:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
主干视图发生在元素的上下文中。为了使此代码正常工作,您必须在构造时将该元素分配给 View,如下所示:
或者将包含产品查找器的特定 DOM 对象分配给 el。作为替代方案,您可以动态生成元素并使用 jQuery 将其附加到 DOM。我都用过。
Backbone 使用 jQuery 委托来指定其事件并使其上下文化。如果您没有为整个视图指定父元素,Backbone 不会正确分配事件管理器。
Backbone views happen within the context of an element. In order for this code to work, you must assign that element to the View at construction time like this:
Or assign to el the specific DOM object that contains the product finder. As an alternative, you can generate the element dynamically and use jQuery to attach it to your DOM. I've used both.
Backbone uses jQuery delegate to specify and contextualize its events. IF you don't specify the parent element for the entire view, Backbone does not assign the event manager correctly.