Oh an I hope you're using Lift 1.1 cause RequestVar's scope for Ajax
functions is preserved after rendering the page. If not you can just
use a SessioVar instead.

On Dec 23, 4:06 pm, Marius <[email protected]> wrote:
> On Dec 23, 1:51 pm, Adam Warski <[email protected]> wrote:
>
>
>
> > Hello,
>
> > > With SetHtml you just provide the parent element ID and the NodeSeq
> > > you want to render as a child of that parent. You're saying you don;t
> > > have the NodeSeq Corresponding to the List but I assume you do have
> > > the list right? So assume this Ajax function:
>
> > > def myCallback(): JsCmd = {
> > > val myList = ...
>
> > > val html: NodeSeq = myList.flatMap(e => <td>{e toString}</td>)
>
> > > SetHtml("all_todos", html)
>
> > > }
>
> > But then I'm embedding view logic into the code - I wouldn't really want to 
> > do that. Especially once the list becomes anything more fancy then a simple 
> > table, I think putting the list-displaying into an html file better.
>
> Think of Snippets as extensions to the markup responsible for
> generating dynamic markup. Hence markup in snippets is very natural.
> Of course one should not abuse this and generate tons of markup from
> scala code. But what I typically do is to use markup template and then
> bind.
>
> <ul id ="all_todos">
>   <lift:TD.list >
>      <li><pref:item></li>
>   </lift:TD.list>
> </u>
>
> Then on server side in your TD snippet you can cache the snippet
> content Xhtml, in this case being:
>  <li><lift:item></li>
> ... you can put this in a RequestVar.
>
> Then from you Ajax function can get this markup template and use bind
> on it such as:
>
> object MyMarkupVar extends RequestVar[NodeSeq](NodeSeq.empty)
>
> class TD {
>
> def myAjaxCallback(): JsCmd = {
>   val myList = ...
>
>   val markUp = MyMarkupVar.get
>
>   val html: NodeSeq = myList.flatMap(e => bind("pref", markup
>      "item" -> e.toString /*or whatever have you*/
>   ))
>
>   SetHtml("all_todos", html)
>
> }
>
> def list(xhtml: NodeSeq): NodeSeq = {
>    MyMarkupVar.set(xhtml)
>    NodeSeq.Empty
>
> }
> }
>
> therefore you don't need to know from Scala code how your template
> looks like.
>
>
>
> > I tried doing something else, expanding your idea.
> > I've put the list-displaying html markup into a template (todo_list.html), 
> > so now I can reuse it to both display the list and pass it to the snippet 
> > which handles and binds the form:
>
> > Now my form looks like this:
>
> > <lift:TD.add form="post">
> > <addForm:form>
> >   <!-- fields and submit button bound here -->
> > </addForm:form>
> > <addForm:list>
> >   <lift:embed what="todo_list" />
> > </addForm:list>
> > </lift:TD.add>
>
> > <!-- some other code -->
>
> > <!-- the list that I want to redraw -->
> > <lift:embed what="todo_list" />
>
> > In TD.add I have then:
> > ...
> > def drawList() = list(chooseTemplate("addForm", "list", form))
> > def reDraw() = SetHtml("all_todos", drawList())
>
> > bind("todo", chooseTemplate("addForm", "form", form),
> > "priority" -> todo.priority.toForm,
> > "desc" -> todo.desc.toForm,
> > "submit" -> submit("New", checkAndSave))
> > )
> > ...
>
> > Is this the right way to go? And is it the "lift" way?
>
> > Anyway I can't test it yet because I can't get my form to be submitted with 
> > ajax. Both surrounding the bind with SHtml.ajaxForm(...) and replacing 
> > submit with ajaxButton causes the form to stop working (the values in the 
> > model aren't updated).
>
> > Adam
>
> > > Br's,
> > > Marius
>
> > > On Dec 23, 10:32 am, Adam Warski <[email protected]> wrote:
> > >> Hello,
>
> > >> I'm just starting with Lift, and there's one thing I can't figure out. I 
> > >> want to modify the ToDo example from the tutorial so that the "Add" 
> > >> button makes an ajax call, adds the element to a list and displays the 
> > >> modified result.
>
> > >> The problem is that I don't know how to redraw the list after the button 
> > >> has been pressed. More generally: how to redraw a page fragment with 
> > >> ajax? I could do that with SetHtml if I had the NodeSeq corresponding to 
> > >> the list, but the button is in an "unrelated" page fragment so it 
> > >> doesn't have it. What's the Lift way to solve such problems?
>
> > >> The page roughly is:
>
> > >> <lift:TD.add form="post">
> > >> <!-- fields an submit button bound here -->
> > >> </lift:TD.add>
>
> > >> <!-- some other code -->
>
> > >> <!-- the list that I want to redraw -->
> > >> <lift:TD.list all_id="all_todos">
> > >> ...
> > >> </lift:TD.list>
>
> > >> I was looking through the list archive but didn't find anything. Sorry 
> > >> if it's a repeated question :)
>
> > >> --
> > >> Adam
>
> > > --
>
> > > You received this message because you are subscribed to the Google Groups 
> > > "Lift" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to 
> > > [email protected].
> > > For more options, visit this group 
> > > athttp://groups.google.com/group/liftweb?hl=en.

--

You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to [email protected].
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.


Reply via email to