locked
Catching events from data bind RRS feed

  • Question

  • Hello:  I'm working with the split app template, and I'm having trouble catching an event, specifically when I try to catch when the user clicks a link.

    On my split.html I have this

    <divclass="itemtemplate"data-win-control="WinJS.Binding.Template">

        <!--- CODE OMMITTED --->

     <divclass="article-content"data-win-bind="innerHTML: description"></div> 

    On my split.js I'm using this code inside the ready function

    varcosa = WinJS.Utilities.query("a");

    cosa.listen("click",this.navegar,false); //navegar is a function I defined

    This works fine if I hard code a tags in my split.html; however the a tags I'm interested in catching are in the data that is fed to the article-content div.  And these are not caught by my JavaScript

    Any help will be greatly appreciated

    Tuesday, November 27, 2012 9:12 PM

Answers

  • Definitely an async timing bug.
    You have to figure out when the render function on the template is run and the element is appended to the window.document.
    This is exactly why there is the onactivated handler and a promise returned by WinJS.UI.ProcessAll()...

    • Proposed as answer by Cobra Tap Saturday, December 1, 2012 10:31 PM
    • Marked as answer by Song Tian Monday, December 3, 2012 8:53 AM
    Saturday, December 1, 2012 10:31 PM
  • Got it!  Just in case anybody is having the same problem, the event handler has to be placed in the _selectionChanged method located in the split.js file
    Monday, December 10, 2012 9:07 PM

All replies

  • Seems this thing has something to do with asynchronous programming.  Just tried my code with a smaller JSON file and it worked fine. 
    Friday, November 30, 2012 5:22 PM
  • Definitely an async timing bug.
    You have to figure out when the render function on the template is run and the element is appended to the window.document.
    This is exactly why there is the onactivated handler and a promise returned by WinJS.UI.ProcessAll()...

    • Proposed as answer by Cobra Tap Saturday, December 1, 2012 10:31 PM
    • Marked as answer by Song Tian Monday, December 3, 2012 8:53 AM
    Saturday, December 1, 2012 10:31 PM
  • Big thank you!  Seems I have a lot of reading ahead
    Sunday, December 2, 2012 5:47 AM
  • Got it!  Just in case anybody is having the same problem, the event handler has to be placed in the _selectionChanged method located in the split.js file
    Monday, December 10, 2012 9:07 PM