简体   繁体   中英

Listen events relative to the @el with backbone.js in the event attribute of a view

I write my view like this :

class Remobs.Views.Element extends Backbone.View

  template: JST['elements/element']

  events:
    'element_rended': 'initImagesDatas'
    'click img.element_image' : 'observeImageEvents'
    'click .more': 'addToBag'
    'click .less': 'decreaseNumber'

  tagName : 'li'

  render: ->
    $(@el).html(@template(element: @model))
    @currentImage = $(@el).find('img')
    @craftBox = null
    $(@el).trigger('element_rended')
    this

And i want to add an element in the events array like this :

events:
   'click @el.find('img')' : 'myfunction'

Am I able to do that? And how can i do that? I am using jQuery.

you can't do 'click @el.find('img')' but 'click img' is an equivalent of what you want to do - so it's much simpler :)

the way it works is that it takes the first word before first space as a event type and everything after that is turned into a jQuery selector. If you pass just an event name without a selector, event will be bound to @el if you pass a selector string it will delegate the event to @el.find selector_string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM