I am tying to some events in the render
function of backbone.js, more specfically a click
, however, I cannot get it to work.
I have a similar (but very simplified) set-up to:
App.ProductView = Backbone.View.extend({
className: 'title_products_tab',
el: $('.title_products_tab'),
events: {
'click .product_list .edit_product' : 'edit',
For the events section of my backbone view. Down within the render function I wish to trigger the bound .edit_product
click
event:
render: function(){
console.log($._data( $('.title_products_tab .product_list .edit_product')[0], "events" ));
$('.title_products_tab .product_list .edit_product').trigger('click');
Which does not work, the _data
for the element is undefined, however, I know the event does work because the rest of my view functions accordingly. I just cannot seem to trigger events in render
or anywhere else on startup.
I thought:
this.trigger('click .product_list .edit_product');
Would work however, again no luck.
I also heard about bindAll
but I heard that it was not a good idea to use this.
So how do I, or rather when do I, trigger startup events for my backbone view?
So you may find this interesting (note: Backbone's code):
var View = Backbone.View = function(options) {
this.cid = _.uniqueId('view');
this._configure(options || {});
this._ensureElement();
this.initialize.apply(this, arguments);
this.delegateEvents();
};
Especially the last part. The events are bound after the initialize method is called, so after your render method is called. The events are not bound yet.
(I guess you could manually call delegateEvents
if you really need to do it the way you do)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With