SuperPlan(12)Winner Seller Server - JSONP Server 14. Setting Up the Winner Seller Server I prefer to try the ice intelliJ IDEA,so I need to add one more plugin in plugin.sbt addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.4.0") addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.2.0") >sbt gen-idea >sbt eclipse Sometimes,I will use eclipse,sometimes,intelliJ. Some useful short cut command for intelliJ ctrl + N ctrl + shift + T show class ctrl + shift + N ctrl + shift + R show file ctrl + G go to line ctrl + alt + O ctrl + shift + O 14.1 Another Way to Write the DAO Codes object NavBars extends Table[NavBar]("NAVBAR") { def id = column[Long]("ID",O.PrimaryKey,O.AutoInc) // 1 This is the primary key column def title = column[String]("NAVBAR_TITLE") // 2 def link = column[String]("NAVBAR_LINK") //3 def alter = column[String]("NAVBAR_ALTER") //4 def parentId = 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) }) def forInsert = title ~ link ~ alter ~ parentId.? <> ({ t => NavBar(None,t._1,{ (s: NavBar) => Some(s.title,s.parentId) }) def insert(s: NavBar)(implicit session: Session): Long = { NavBars.forInsert returning id insert s } …snip… case class NavBar(id: Option[Long],title: String,link: String,alter: String,parentId: Option[Long],subs: Option[List[NavBar]],parent: Option[NavBar]) 14.2 Method to Check the Table Exist val tableList = MTable.getTables.list(db) if(!tableList.contains(Users.tableName)){ Users.create } 14.3 Understanding of implicit Implicit Transfer My understanding is that if there is type mismatch,it will look up the implicit method to transfer. Implicit Parameter If you define an implicit parameter in the function,it will look up the implicit parameters first in scope. 14.4 Logging System import scala.slick.util.Logging trait AuthenticationDirectives extends Logging { … } import com.typesafe.scalalogging.slf4j.Logging object SchemaSetup extends Logging{ … } 15. BackBone I do not like to use json,I prefer jsonp instead. Yes,I made it using JSONP instead of JSON. And I am also using BackBone. For the server side,I am using spray route. The core codes should be like this. def route = { 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("Hitting the URI navbars with apiVersion=" + apiVersion + ",brandCode=" + brandCode) DefaultJsonProtocol.listFormat[NavBar].write(dao.NavBars.all).toString } )) // } // } } } ~ …snip… the most important part is jsonpWithParameter,there is no much document about this,I read the source codes to figure out how to integrate them together. For the client side,I am using backbone collection. The codes should be as follow: define([ 'under
score','backbone' ],function( _,Backbone) { var Items = Backbone.Collection.extend({ url: '/navbars',parse: function(response) { window.logger.debug("getting NavBars from response=" + response); return response; },sync: function(method,model,options){ options.timeout = 10000; //json mock //options.url = 'http://localhost:9000/data' + "/navbars" + ".JSON"; //options.dataType = 'json'; //jsonp //options.headers = "{ 'Authorization' : 'Basic Y3VzdG9tZXI6Y3VzdG9tZXI=' }"; options.dataType = "jsonp"; options.crossDomain = true; options.url = 'http://localhost:9002/v1/sillycat' + "/navbars"; return Backbone.sync(method,options); } }); return Items; }); Maybe,in the future,I can switch from json mock to jsonp server. Actually,I implement a way to do the authentication in spray server side,I am using basic auth. But I am using it in the wrong way that I can not go to the basic auth from http://username:password@server way. The next step for is to change this. 16. Jasmine come soon... 17. Integration(Backbone + Require + Jasmine + Phantom + Grunt + Bootstrap) come soon... Tips: 1. OutOfMemory When I run the command sbt>test,I got this kind of error message. java.lang.OutOfMemoryError: PermGen space Solution: >vi ~/.sbtconfig export SBT_OPTS=-XX:MaxPermSize=256M References: https://github.com/mohitjain/learning_basics_backbone grunt sample 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 http://sillycat.iteye.com/blog/1775972 jsonp http://sillycat.iteye.com/blog/642858 http://spray.io/documentation/spray-routing/ https://github.com/spray/spray/wiki/Misc-Directives jsonp with basic auth http://kevinkuchta.com/_site/2012/01/basic-authentication-with-jsonp/
原文链接:https://www.f2er.com/json/290467.html