SuperPlan(12)Winner Seller Server - JSONP Server
SuperPlan(12)WinnerSellerServer-JSONPServer
14.SettingUptheWinnerSellerServer
IprefertotrytheiceintelliJIDEA,soIneedtoaddonemoreplugininplugin.sbt
addSbtPlugin("com.github.mpeltonen"%"sbt-idea"%"1.4.0")
addSbtPlugin("com.typesafe.sbteclipse"%"sbteclipse-plugin"%"2.2.0")
>sbtgen-idea
>sbteclipse
Sometimes,Iwilluseeclipse,sometimes,intelliJ.
SomeusefulshortcutcommandforintelliJ
ctrl+Nctrl+shift+Tshowclass
ctrl+shift+Nctrl+shift+Rshowfile
ctrl+Ggotoline
ctrl+alt+Octrl+shift+O
14.1AnotherWaytoWritetheDAOCodes
objectNavBarsextendsTable[NavBar]("NAVBAR"){
defid=column[Long]("ID",O.PrimaryKey,O.AutoInc)//1Thisistheprimarykeycolumn
deftitle=column[String]("NAVBAR_TITLE")//2
deflink=column[String]("NAVBAR_LINK")//3
defalter=column[String]("NAVBAR_ALTER")//4
defparentId=column[Long]("PARENT_ID")//5
def*=id.?~title~link~alter~parentId.?<>
({t=>NavBar(t._1,t._2,t._3,t._4,t._5,None,None)},
{(s:NavBar)=>Some(s.id,s.title,s.link,s.alter,s.parentId)})
defforInsert=title~link~alter~parentId.?<>
({t=>NavBar(None,t._1,t._2,t._3,t._4,None,None)},
{(s:NavBar)=>Some(s.title,s.link,s.alter,s.parentId)})
definsert(s:NavBar)(implicitsession:Session):Long={
NavBars.forInsertreturningidinserts
}
…snip…
caseclassNavBar(id:Option[Long],title:String,link:String,alter:String,parentId:Option[Long],subs:Option[List[NavBar]],parent:Option[NavBar])
14.2MethodtoChecktheTableExist
valtableList=MTable.getTables.list(db)
if(!tableList.contains(Users.tableName)){
Users.create
}
14.3Understandingofimplicit
ImplicitTransfer
Myunderstandingisthatifthereistypemismatch,itwilllookuptheimplicitmethodtotransfer.
ImplicitParameter
Ifyoudefineanimplicitparameterinthefunction,itwilllookuptheimplicitparametersfirstinscope.
14.4LoggingSystem
importscala.slick.util.Logging
traitAuthenticationDirectivesextendsLogging{
…
}
importcom.typesafe.scalalogging.slf4j.Logging
objectSchemaSetupextendsLogging{
…
}
15.BackBone
Idonotliketousejson,Ipreferjsonpinstead.Yes,ImadeitusingJSONPinsteadofJSON.
AndIamalsousingBackBone.Fortheserverside,Iamusingsprayroute.Thecorecodesshouldbelikethis.
defroute={
pathPrefix(Version/BrandCode){(apiVersion,brandCode)=>
//authenticate(customerOnly){user=>
path("navbars"){
//respondWithMediaType(`application/json`){
//get{
jsonpWithParameter("callback"){
//complete(HttpBody(`application/json`,"""{"key":"value"}"""))
complete(HttpBody(`application/json`,
dao.db.withSession{
logger.debug("HittingtheURInavbarswithapiVersion="+apiVersion+",brandCode="+brandCode)
DefaultJsonProtocol.listFormat[NavBar].write(dao.NavBars.all).toString
}
))
//}
//}
}
}~
…snip…
themostimportantpartisjsonpWithParameter,thereisnomuchdocumentaboutthis,Ireadthesourcecodestofigureouthowtointegratethemtogether.
Fortheclientside,Iamusingbackbonecollection.Thecodesshouldbeasfollow:
define([
'underscore',
'backbone'
],function(_,Backbone){
varItems=Backbone.Collection.extend({
url:'/navbars',
parse:function(response){
window.logger.debug("gettingNavBarsfromresponse="+response);
returnresponse;
},
sync:function(method,model,options){
options.timeout=10000;
//jsonmock
//options.url='http://localhost:9000/data'+"/navbars"+".JSON";
//options.dataType='json';
//jsonp
//options.headers="{'Authorization':'BasicY3VzdG9tZXI6Y3VzdG9tZXI='}";
options.dataType="jsonp";
options.crossDomain=true;
options.url='http://localhost:9002/v1/sillycat'+"/navbars";
returnBackbone.sync(method,model,options);
}
});
returnItems;
});
Maybe,inthefuture,Icanswitchfromjsonmocktojsonpserver.
Actually,Iimplementawaytodotheauthenticationinsprayserverside,Iamusingbasicauth.ButIamusingitinthewrongwaythatIcannotgotothebasicauthfromhttp://username:password@serverway.
Thenextstepforistochangethis.
16.Jasmine
comesoon...
17.Integration(Backbone+Require+Jasmine+Phantom+Grunt+Bootstrap)
comesoon...
Tips:
1.OutOfMemory
WhenIrunthecommandsbt>test,Igotthiskindoferrormessage.
java.lang.OutOfMemoryError:PermGenspace
Solution:
>vi~/.sbtconfig
exportSBT_OPTS=-XX:MaxPermSize=256M
References:
https://github.com/mohitjain/learning_basics_backbone
gruntsample
http://gruntjs.com/sample-gruntfile
integration
http://hdnrnzk.me/2013/01/10/backbone-requirejs-jasmine-phantomjs-and-grunt/
https://github.com/ghiden/backbone-requirejs-jasmine-phantomjs-grunt-setup
implicit
jsonp
http://spray.io/documentation/spray-routing/
https://github.com/spray/spray/wiki/Misc-Directives
jsonpwithbasicauth
http://kevinkuchta.com/_site/2012/01/basic-authentication-with-jsonp/