SHtml.idMemoize

20 views
Skip to first unread message

Andreas Joseph Krogh

unread,
Aug 15, 2017, 4:43:17 PM8/15/17
to lif...@googlegroups.com
Hi.
 
I very often need to use SHtml.idMemoize in places where I want to replace the markup, rather than setting its innerHTML (which is what SHtml.idMemoize does).
 
I have a custom variant doing just that:
def idMemoizeReplace(f: IdMemoizeTransform => NodeSeqFuncOrSeqNodeSeqFunc): IdMemoizeTransform = {
   new IdMemoizeTransform {
      var latestElem: Elem = <span/>
      val latestKids: NodeSeq = NodeSeq.Empty

      var latestId = Helpers.nextFuncName

      private def fixElem(e: Elem): Elem = {
         e.attribute("id") match {
            case Some(id) => latestId = id.text ; e
            case None => e % ("id" -> latestId)
         }
      }

      def apply(ns: NodeSeq): NodeSeq =
         Helpers.findBox(ns){e => latestElem = fixElem(e); Full(e)}.
         map(ignore => applyAgain()).openOr(NodeSeq.Empty)

      def applyAgain(): NodeSeq =
         f(this)(latestElem)

      def setHtml(): JsCmd = Replace(latestId, f(this)(latestElem))
   }
}
Does anybody find this useful and should we consider adding something like this to SHtml?
 
--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963

Antonio Salazar Cardozo

unread,
Aug 16, 2017, 3:04:44 PM8/16/17
to Lift
Personally I would rather `IdMemoizeTransform` become a class that
allows the instantiator to define how setHtml will be constructed, probably
as a (latestId, latestElem, latestKids)=>JsCmd function. That feels like it'll
provide a bit more flexibility than our current approach, and probably produce
less metaspace usage/GC to boot (since anonymous subclasses like those
used by idMemoize right now require metaspace to create).
Thanks,
Antonio
Reply all
Reply to author
Forward
0 new messages