It's ugly, but this works: (new java.util.ArrayList[Subscription](items)).flatMap(...)
I guess that Scala is uncomfortable imposing order on something that's not explicitly ordered? Kris On Wed, Sep 10, 2008 at 4:23 PM, Kris Nuttycombe <[EMAIL PROTECTED]> wrote: > I quickly found out that Group didn't work, but I'm still stymied; why > does Conversions provide converters for Set, List, and Map, but not > your basic everyday java.util.Collection? My existing code was using > an implicit conversion of my own creation: > > implicit def collToWrapper[A](coll : java.util.Collection[A]) = new > CollectionWrapper[A] {override def underlying = coll} > > but that doesn't solve the problem. The reason that I'm using the > java.util collections is because all of my model classes are EJB3 > entities, with most of the associations between models wired as > java.util.Collection. > > Thanks, > > Kris > > On Wed, Sep 10, 2008 at 4:11 PM, Jorge Ortiz <[EMAIL PROTECTED]> wrote: >> >> Group won't work, as it too needs a Seq[Node], an Iterable[Node] just won't >> do. >> >> Kris, this problem is certainly annoying and stems from the type >> differences between Java collections and Scala collections. You need >> your "items" argument to either be a proper Scala collection (Seq or >> any of it's subclasses (List, Array, etc)) or you can wrap your Java >> collections with the wrappers in scala.collections.jcl (the >> Conversions object has some convenient implicit conversions). These >> wrappers all extend Scala's Seq, which is what you want. >> >> --j >> >> On Wed, Sep 10, 2008 at 2:51 PM, Marius <[EMAIL PROTECTED]> wrote: >>> >>> Kris, >>> >>> Having a template like (slightly changed from your example so the >>> values to be dynamic): >>> >>> <lift:Hello.orderList> >>> <order:id/> >>> <order:items> >>> <item:name/> >>> <item:cost/> >>> </order:items> >>> </lift:Hello.orderList> >>> >>> >>> and the Scala code: >>> >>> case class OrderItem(name:String, cost:String) >>> >>> def orderList(xhtml: Group): NodeSeq = { >>> >>> val orderList = OrderItem("first", "9.99$") :: OrderItem("second", >>> "19.99$") :: Nil >>> >>> val content = (xhtml \\ "items").first.descendant >>> >>> println("Content " + content) >>> >>> bind("order", xhtml, >>> "id" -> Text("My order Id"), >>> "items" -> Group(orderList flatMap (elem => bind("item", >>> content, "name" -> Text(elem.name), "cost" -> Text(elem.cost)))) >>> ) >>> >>> } >>> should work just fine (at least for me it does). >>> >>> .... regarding your compile error the flatMap call returns an >>> Iterable[scala.xml.Node] which does not conform with NodeSeq return >>> Type. To fix this you can probably wrap everything into a Group like: >>> >>> private def bindItems(xhtml : NodeSeq, >>> items :java.util.Collection[Item]) : NodeSeq = { >>> Group(items.flatMap(item => >>> bind("item", xhtml, >>> "name" --> Text(item.getName)))) >>> } >>> >>> ... just out of curiosity is there a reason why you;re using >>> java,util.collection and not Scala List, Seq etc? >>> >>> >>> Br's, >>> Marius >>> >>> On Sep 11, 12:16 am, "Kris Nuttycombe" <[EMAIL PROTECTED]> >>> wrote: >>>> Man, I really wish I understood the Scala type system better. >>>> >>>> def list(xhtml : NodeSeq) : NodeSeq = { >>>> val orders = EM.createQuery[Order]("FROM Order").getResultList >>>> orders.flatMap(order => >>>> bind("order", xhtml, >>>> "id" --> Text(order.getId.toString), >>>> "uuid" --> Text(order.getUuid.toString), >>>> "items" --> bindItems((xhtml \\ "items").first.child, >>>> order.getItems)) >>>> ) >>>> } >>>> >>>> private def bindItems(xhtml : NodeSeq, items : >>>> java.util.Collection[Item]) : NodeSeq = { >>>> items.flatMap(item => >>>> bind("item", xhtml, >>>> "name" --> Text(item.getName))) >>>> } >>>> >>>> [WARNING] >>>> /home/knuttycombe/work/gcsi/gcsi-admin/src/main/scala/com/gaiam/gcsi/snippet/Orders.scala:33: >>>> error: type mismatch; >>>> [WARNING] found : Iterable[scala.xml.Node] >>>> [WARNING] required: scala.xml.NodeSeq >>>> [WARNING] items.flatMap(item => >>>> [WARNING] ^ >>>> [WARNING] one error found >>>> >>>> That's the only error, too; the "list" function compiles just fine. >>>> I've tried making that items.flatMap[NodeSeq], adding an explicit cast >>>> .asInstanceOf[NodeSeq] to the results of the binding, and split >>>> bindItems out into a separate function just to try to make things look >>>> as much the same as possible. WTF? >>>> >>>> Kris >>>> >>>> On Wed, Sep 10, 2008 at 2:28 PM, Derek Chen-Becker >>>> >>>> <[EMAIL PROTECTED]> wrote: >>>> > Sorry, forgot about that. You might be able to use Scala's XML >>>> > processing to >>>> > just get at the children: >>>> >>>> > bind ("item", (xhtml \\ "items").first.child, ...) >>>> >>>> > Kind of ugly. I still can't find the example that David posted but it >>>> > seemed >>>> > a lot cleaner. >>>> >>>> > Derek >>>> >>>> > On Wed, Sep 10, 2008 at 12:39 PM, Kris Nuttycombe >>>> > <[EMAIL PROTECTED]> wrote: >>>> >>>> >> Hrm. I think that I need to do something extra there to extract and >>>> >> bind against just the <order:items> element to avoid re-binding over >>>> >> the entire contents of the snippet. >>>> >>>> >> Marius, can you expand at all on using snippet attributes for case >>>> >> (1)? I've googled around and looked at all of the template docs I can >>>> >> find and am not really following how to take that approach. >>>> >>>> >> Thanks, >>>> >>>> >> Kris >>>> >>>> >> On Wed, Sep 10, 2008 at 12:08 PM, Derek Chen-Becker >>>> >> <[EMAIL PROTECTED]> wrote: >>>> >> > I think the bind would look something like this (assuming that xhtml >>>> >> > is >>>> >> > the >>>> >> > input NodeSeq): >>>> >>>> >> > bind("order", xhtml, "id" --> Text(your_id_here), >>>> >> > "items" --> your_items.flatMap{ item => >>>> >> > bind("item", >>>> >> > xhtml, "name" --> Text(item.name), "cost" --> Text(item.cost)) }) >>>> >>>> >> > I seem to remember David giving an example of a nested bind but I >>>> >> > can't >>>> >> > find >>>> >> > it. >>>> >>>> >> > Derek >>>> >>>> >> > On Wed, Sep 10, 2008 at 11:37 AM, Marius <[EMAIL PROTECTED]> >>>> >> > wrote: >>>> >>>> >> >> Hi, >>>> >>>> >> >> 1. You can use snippet attributes. >>>> >> >> 2. You can leave the divs in the template page and "attach" only >>>> >> >> certain attributes from the snippet. Note that Scala XML API is >>>> >> >> immutable but that was never an inconvenience for me :) ... you could >>>> >> >> pattern match them easily. >>>> >>>> >> >> Br's, >>>> >> >> Marius >>>> >>>> >> >> On Sep 10, 8:05 pm, "Kris Nuttycombe" <[EMAIL PROTECTED]> >>>> >> >> wrote: >>>> >> >> > Hi, all, >>>> >>>> >> >> > I'm in the process of moving a Rails app over to Lift, and have a >>>> >> >> > couple of questions about how to achieve a few things that aren't >>>> >> >> > really addressed in the basic template tutorials. >>>> >>>> >> >> > First, I have a situation where I have a list of orders, each of >>>> >> >> > which >>>> >> >> > has some number of items. What would the binding look like for >>>> >> >> > something like the following? >>>> >>>> >> >> > <lift:snippet type="Orders:list"> >>>> >> >> > <order:id>sample_order_id</order:id> >>>> >> >> > <order:items> >>>> >> >> > <item:name>Sample Item</item:name> >>>> >> >> > <item:cost>$0.00</item:cost> >>>> >> >> > </order:items> >>>> >> >> > </lift:snippet> >>>> >>>> >> >> > Secondly, I know that there have been some additions since the >>>> >> >> > discussion on dynamic node attributes >>>> >>>> >> >> > here:http://groups.google.com/group/liftweb/browse_thread/thread/c7fe7b4b3... >>>> >>>> >> >> > What is the best way to go about porting something that looks like >>>> >> >> > this in Rails: >>>> >>>> >> >> > <div style="color:blue;cursor:pointer" >>>> >> >> > onclick="document.getElementById('order_<%= >>>> >> >> > order.id%>').style.display='block';this.style.display='none'">[View >>>> >> >> > Details]</div> >>>> >>>> >> >> > <div id="order_<%= order.id %>" style="display:none"> >>>> >> >> > ... >>>> >> >> > </div> >>>> >>>> >> >> > It seems a bit odd to use different snippet functions to write the >>>> >> >> > onclick and id attributes. I know that I could move the generation >>>> >> >> > of >>>> >> >> > both divs into the snippet, but that won't make our designers happy >>>> >> >> > because they won't be able to play with the functionality in their >>>> >> >> > browsers. >>>> >>>> >> >> > Thanks, >>>> >>>> >> >> > Kris >>> > >>> >> >> >> >> > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Lift" group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~----------~----~----~----~------~----~------~--~---