evanmoran / oj

Unified web templating for the people. Thirsty people.
MIT License
445 stars 7 forks source link

insert + click events self-triggering #14

Closed spencermountain closed 10 years ago

spencermountain commented 10 years ago

hey evan, found something curious happening when there's an insert, followed by a click listener - the click listener triggers somehow during the insert. if you change either one to another listener, or even change the order of the listeners, it works as expected. cheers

  $("body").oj(
    div {
      insert:->
        console.log "insert"
      click:->
        alert('this runs on insert')
    }
  )
evanmoran commented 10 years ago

Good catch. Definitely something is happening there, so I'll have to look into it. It appears if you reverse the order it works correctly:

$("body").oj ->
  div 'hello',
    click: ->
      alert('this runs on click')
    insert: ->
      alert('this runs on insert')
    alert('this runs on compile')
evanmoran commented 10 years ago

Ah, interesting it is definitely calling the last defined event at a strange time. For example this one is calling mouseout when inserting:

Button 'Insert', click: ->
  $("body").ojAppend ->
    div 'hello',
      insert: ->
        alert('this runs on insert')
      click: ->
        alert('this runs on click')
      mouseout: ->
        alert('this runs on out')
    alert('this runs on compile')
evanmoran commented 10 years ago

Thanks for finding this =). This is fixed in 0.3.3. Let me know if it works for you!

(If you are interested: the issue was the classic function bind issue where the last variable in the loop was captured instead of the specific insert one).