jahilldev
jahilldev

Reputation: 3812

Scala Lift - List() as JsObj child value

I have the following model:

class Chat private() extends MongoRecord[Chat] with ObjectIdPk[Chat] {

    def meta = Chat

    object room extends StringField(this, 50) 
    object user extends StringField(this, 50)
    object name extends StringField(this, 50)
    object level extends StringField(this, 50)
    object target extends StringField(this, 50)
    object value extends StringField(this, 50)
    object time extends StringField(this, 50)
    object fulltime extends StringField(this, 50)
    object handle extends StringField(this, 50)

}

object Chat extends Chat with MongoMetaRecord[Chat] {

    override def collectionName = "chat"

}

Which I load using:

var chat_model = Chat.findAll(

    ("room" -> "testroom"),
    ("time" -> 1)

).map(_.asJValue)

Which I render in a snippet using:

def render = {

    <script type="text/javascript">

        var DATA = {JsObj(

            ("CHAT", chat_model)

        )}

    </script>

}

Which gives a compile error:

[error] overloaded method value apply with alternatives:
[error]   (in: net.liftweb.http.js.JsExp*)net.liftweb.http.js.JE.JsArray <and>
[error]   (in: List[net.liftweb.http.js.JsExp])net.liftweb.http.js.JE.JsArray
[error]  cannot be applied to (List[net.liftweb.json.JsonAST.JObject])
[error]                                 ("CHAT", JsArray(chat_model)

If I pass just the first item in the chat_model, like:

JsObj(

    ("CHAT", chat_model(0))

)

It works fine but obviously doesnt print the whole array of objects.

Thanks in advance for any help, much appreciated :)

Upvotes: 0

Views: 792

Answers (2)

leedm777
leedm777

Reputation: 24062

Looking at the JsObject.apply method, it takes parameter of (String, JsExp)*. You're giving it (String, List[JObject]). You need to convert that list to a JsExp.

You can wrap the list in a JArray, which should implicitly convert to a JsExp.

import net.liftweb.json.JsonAST._

JsObj(("CHAT", JArray(chat_model)))

Upvotes: 1

Dave Whittaker
Dave Whittaker

Reputation: 3102

It seems to me that there is an implicit conversion from JObject to JsExp in the scope of your code, but that won't convert a List[JObject] to a List[JsExp]. Try changing your map to something like:

map(_.asJValue : JsExp)

I think that should be enough to trigger conversion to a JsExp for each element.

Upvotes: 0

Related Questions