Buttons not calling the JsCmd function registered to

20 views
Skip to first unread message

sayo9394

unread,
Oct 27, 2010, 11:06:32 PM10/27/10
to Lift
Hi all,

First, I'm a completely new when it comes to scala and liftweb. It is
a uni subject for this semester and coming from a C/C++ background,
lets say i'm having some difficulties understanding this.

The project i'm working on is a simplified Reddit or Digg clone. I
currently have a "LinkActor.scala" extending CometActor which has a
list of links submitted by users offering a "Vote Up" button and "Vote
Down" button options to the user. The code compiles and run, but when
i click on the vote up or down buttons, nothing happen. the console
doesn't print any requests or error messages. I tried both firefox and
chrome (developing on Ubuntu 10.04)

The Actor; http://github.com/sayo9349/Scala_Reddit_Clone/blob/clone_with_comet/src/main/scala/com/redditclone/comet/LinkActor.scala
The Controller:
http://github.com/sayo9349/Scala_Reddit_Clone/blob/clone_with_comet/src/main/scala/com/redditclone/controller/RedditClone.scala
The code is available here; http://github.com/sayo9349/Scala_Reddit_Clone
(feel free to download and check it out)

Thank you very much for your help guys.

cheers,
Simon

LinkActor.scala;

package com.redditclone.comet
import net.liftweb.http._
import net.liftweb.http.S._
import net.liftweb.http.SHtml._
import net.liftweb.http.js.JsCmd
import net.liftweb.http.js.JsCmds._
import net.liftweb.http.js.JE._
import net.liftweb.util.Helpers._
import net.liftweb.util._
import scala.xml.{NodeSeq,Text}
import com.redditclone.model._
import com.redditclone.controller._
import java.lang.Long

class LinkActor extends CometActor {
override def defaultPrefix = Full("linkactor")
var lnkViews: List[ReditLink] = Nil
def render = {
def lnkView(lnk: ReditLink): NodeSeq = {
val rank = lnk.rank
val voteUpButton = <button type="button">{S.?("Vote Up!")}
</button> %
("onclick" -> ajaxCall(JsRaw(lnk.title.is), voteUp
_))
val voteDownButton = <button type="button">{S.?("Vote
Down!")}</button> %
("onclick" -> ajaxCall(JsRaw(lnk.title.is), voteDown
_))

(<div>
<strong>Title:</strong> {lnk.title}
<br/>
<strong>Rank:</strong> {lnk.rank}
<br/>
<div>
<a href={"/reditLink/" + lnk.title.is}
>{lnk.title.is}</a>: by {lnk.owner.name}
</div>
<div>
{voteUpButton} {voteDownButton}
</div>
<strong>Description:</strong> {lnk.description}<br/
><br/>
</div>)
}
bind("foo" -> <div>{lnkViews.flatMap(lnkView _)}</div>)
}

def voteUp(title:String): JsCmd = {
val user = User.currentUser.open_!
ReditClone ! VoteUp(title,user)
Noop
}

def voteDown(title:String): JsCmd = {
val user = User.currentUser.open_!
println("title: "+title)
println("user: "+user)
ReditClone ! VoteDown(title,user)
Noop
}

override def localSetup {
ReditClone !? AddListener(this) match {
case UpdateLinks => lnkViews = ReditLink.findAllLinks
case _ => println("Other ls")
}
}

override def localShutdown {
ReditClone ! RemoveListener(this)
}

override def lowPriority : PartialFunction[Any, Unit] = {
case UpdateLinks => lnkViews = ReditLink.findAllLinks;
reRender(false)
case _ => println("Other lp")
}
}

David Pollak

unread,
Oct 28, 2010, 12:21:05 AM10/28/10
to lif...@googlegroups.com
I've updated your code to Lift 2.1 and Scala 2.8.0: http://github.com/dpp/Scala_Reddit_Clone

I'm not sure why the code was failing, but I have a couple of ideas:
  • Your ajaxCall was passing a JsRaw(title) which turned into JavaScript that was referencing a browser-side variable like twitter which was probably causing an exception (you can use Firebug to debug scripts, it works like a charm)
  • You were doing open_! on users and if the user wasn't logged in, that would cause an exception.  Scala Actors stop running if an exception is thrown and they are pretty silent about Exceptions (one of the many reasons we moved to our own Actor implementation)
  • You were doing all lookups with String which are notoriously unreliable and are notoriously hackable (think parameter tampering).  I updated the button generation to use SHtml.ajaxButton which takes a function that closes over scope (the id of the current link), so the call is safe and there's no messing about with Strings
I hope this helps.


--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Blog: http://goodstuff.im
Surf the harmonics

Simon Ayoub

unread,
Oct 28, 2010, 4:00:03 AM10/28/10
to lif...@googlegroups.com
Thank you so much David,

i have managed to merge the changes into the branch (http://github.com/sayo9349/Scala_Reddit_Clone), and after running mvn jetty:run, and after it downloaded and updates all the required libraries, it gave me the following error;

ERROR - Failed to find a properties file (but properties were accessed)

and some other java Exceptions such as;
java.sql.SQLException: Failed to create database 'lift_example',
ERROR - Failed to Boot! Your application may not run properly
java.lang.NullPointerException:

I'm guessing i'm missing a property file that wasn't created when i did the mvn archetype:generate a while ago...

Thank you again for your help.

Simon

Simon Ayoub

unread,
Oct 28, 2010, 4:05:01 AM10/28/10
to lif...@googlegroups.com
I found the ticket about the error, #469, basic archetype shows error during boot, and while checking that revision, i added the following file; src/main/resources/props/default.props 
now i'm left with the following Java Exception errors;
java.sql.SQLException: Failed to create database 'lift_example', see the next exception for details.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.seeNextException(Unknown Source) ......
Caused by: java.sql.SQLException: Directory /home/simon/work/lift/Scala_Reddit_Clone/lift_example already exists.
Caused by: ERROR XBM0J: Directory /home/simon/work/lift/Scala_Reddit_Clone/lift_example already exists.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
ERROR - Failed to Boot! Your application may not run properly
java.lang.NullPointerException: Looking for Connection Identifier ConnectionIdentifier(lift) but failed to find either a JNDI data source with the name lift or a lift connection manager with the correct name
at net.liftweb.mapper.DB$$anonfun$7$$anonfun$apply$12.apply(DB.scala:141)
at net.liftweb.mapper.DB$$anonfun$7$$anonfun$apply$12.apply(DB.scala:141)
at net.liftweb.common.EmptyBox.openOr(Box.scala:465)

cheers,
Simon

Simon Ayoub

unread,
Oct 28, 2010, 4:37:14 AM10/28/10
to lif...@googlegroups.com
Considering that the data in the database isn't important, and since the java SQLExceptions all pointed at lift_example already exists, I simply deleted the database folder and now it's working.

Sorry for flooding you with all these emails. I should have been more patient, relaxed a little, then look at the problem at hand.

Cheers,
Simon
Reply all
Reply to author
Forward
0 new messages