Andriy Drozdyuk
Andriy Drozdyuk

Reputation: 61121

Really confused about snippets

I have a problem with my lift view. The thing is, I am making an expensive remote rest-api call twice - where I should really need to do it only once.

But I can't figure out how to solve this.

Basically I have an HTML template like this, that needs to display the list of users and their count:

//UserSearchResults.html

Num users: <span class="lift:UserSearchResults.userCount"></span>

User list:
<ul>
<lift:UserSearchResults.userList>   
   <li><user:userName/></li>
</lift:UserSearchResults.userList>    
</ul>

And then I have an actual snippet that goes and retrieves the list of users from the rest-api server. However, note that it actually does this TWICE - once to count the number of users, and once to render the list.

  //UserSearchResults.scala 

  /** Get list of users from api */
  def users: List[User] = {
    val url = "http://server/rest-api/user-search";
    val result = io.Source.fromURL(url).mkString

    //... parse users into List[User] and return it

    return entries
  }

  /** Render user count */
  def userCount =
    "* *" #> users.length    //<-- ONE call

  def userList(in: NodeSeq): NodeSeq = {
    users.flatMap(user => Helpers.bind("user", in,  //<--SECOND call
      "userName" -> user.user_name)) 
  }

Is there a better place to put the api call? Is there like a "constructor" for the snippet, that I can use cache the user list, and to share it across all the functions in the class?

Any help is appreciated.

Upvotes: 2

Views: 248

Answers (2)

If UserSearchResults is a class (as opposed to an object), then there will be a per-request instance of that class. As such, all you have to do is change your def users to a lazy val users and you should be good to go.

Upvotes: 5

Kim Stebel
Kim Stebel

Reputation: 42047

If your snippet extends StatefulSnippet, you can just save the list in an instance variable. Another option would be to put the list into a RequestVar. Then it could also be accessed from other snippets.

Upvotes: 0

Related Questions