Should be using `target` rather than `srcElement` in Scala.js -
i have scala.js code works fine when target-browser chrome not when firefox. have been told should not use srcelement
instead target
. can't work out how access target
.
private def td(locid: location, personid: person): htmltablecellelement = { val res = td(style := mypeopletable_td).render res.onclick = { (e: dom.event) => c.clicktd(e.srcelement)} res.id = id(locid, personid).tostring cells += (id(locid, personid) -> res) res } def clicktd(element: element): unit = { val idoption = id.parse(element.id) idoption.foreach(id => { locidx = location.indexof(id.locid) personidx = person.indexof(id.personid) localsync() v.personvaluebox.focus() }) }
by way of explanation c
controller , v
view. first method in view , and second in controller. first method setting cell in html table, including happens when user clicks on cell. in clicktd()
code needs know has been clicked - needs somehow @ html element's 'id' has been pre-set model objects can looked up.
if try replace e.srcelement
e.target
result:
[info] compiling 1 scala source c:\dev\v2\atmosphere\js\target\scala2.11\classes... [error] c:\dev\v2\atmosphere\js\src\main\scala\simple\view.scala:145: type mismatch; [error] found : org.scalajs.dom.raw.eventtarget [error] required: org.scalajs.dom.element [error] (which expands to) org.scalajs.dom.raw.element [error] res.onclick = { (e: dom.event) => c.clicktd(e.target)} [error] ^ [error] 1 error found [error] (atmospherejs/compile:compile) compilation failed [error] total time: 1 s, completed 15-jun-2015 02:14:41
thanks @ochrons, suggestion of putting res
instead of e.target
works. unfortunately other controls i'm having 'order of instantiation' problem. in following trying move commented-out line can see res
:
private def button_td_tog(idt: row): htmltablecellelement = { val idsstr: string = idt.tostring val buttonwidget = button( style := mytogglebutton, name := "button", id := idsstr //onclick := { (e: dom.event) => c.clicktogglebutton(e.srcelement)} ) val res = td(style := mytogglebuttoncol, buttonwidget).render buttonwidget.onclick = { (e: dom.event) => c.clicktogglebutton(res)} res.id = idsstr buttontds += (idt -> res) res }
i (think i) need both res
, buttonwidget
exist before assigning onclick, compiler not recognize onclick. found myself searching api documentation me out here. doubt exists (i've found scala.js documentation excellent, not comprehensive). perhaps should looking dom itself...
and example of simple general solution seems work cases, per advice force types:
c.clickpushbutton(e.target.asinstanceof[dom.element])}
if need know td
element has been clicked, can directly use c.clicktd(res)
in closure. if there elements within td
can clicked, can use event's target
casting element
. in such case inner elements must bubble click event td
element.
the dom not entirely type-safe, need "know" dealing with. documentation typically assumes javascript, don't care things type-safety.
Comments
Post a Comment