Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone View events not firing

I am using Rails3 as my backend and Jammit to asset... Now I am trying to not compress and even package the asstes.

The simple event don't get run, but the alert('asd') int inialize is working as expected.

I have already tryed 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>
like image 604
pedrofs Avatar asked Jun 18 '11 00:06

pedrofs


People also ask

How do I trigger an event in Backbone JS?

js trigger Event is used to invoke or callback the function for the given event or a space-delimited list of events. The subsequent arguments will be passed along to the event callbacks in order to trigger it. Parameter Values: event: It is used to bind an object with an event.

How does backbone js work?

Backbone. js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

What is backbone in programming?

Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).

Why use backbone js?

BackboneJS allows developing of applications and the frontend in a much easier way by using JavaScript functions. BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications.


1 Answers

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:

var InvoiceStock = Backbone.View.extend({
    el: $('#product_finder').parent()

...

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.

like image 128
Elf Sternberg Avatar answered Sep 21 '22 00:09

Elf Sternberg