Comments (7)
I do not really understand where the implicit class will be added, I created a Implicit ActiveDatabase for the default database,
class ActiveDatabase(name : String) {
val dbName = name
}
package object globals {
implicit lazy val database = new ActiveDatabase("default")
}
But I don't really understand how the implicit parameters work
from eatsafe.
First of all, note that the class can be simplified to merely class ActiveDatabase(val name: String)
. That will do the same as you've written, but the parameter will be name
.
Anyway, now when you import globals
(should be named Globals
for consistency), the implicit value is in scope of the entire file. So then our model functions would take in a curried, implicit parameter (implicit parameters have to be curried).
Sample syntax: def getLocation(locationId: Int)(implicit activeDb: ActiveDatabase): Try[Location]
. But it would be used the exact same way (ie, we don't have to specify the implicit parameter -- it'll be passed automatically).
We'd then use the variant of play.api.db.DB.withConnection
that takes in the database name as a parameter (see the docs).
EDIT: The above link isn't working because Github breaks it... Copy it from here: https://www.playframework.com/documentation/2.0/api/scala/play/api/db/DB$.html
.
from eatsafe.
This is what I have been trying to do but have been running into problems.
I changed the getLocation exactly as you just said, but then the calls in the controller need a implicit database, which I cannot seem to get from just importing the globals._
from eatsafe.
You imported that in the controller, right? If you're getting an error, what is it?
from eatsafe.
globals._ is imported in LocationController but there is an error
def showLocation(locationId: Int) = Action {
Location.getLocationById(locationId) match {
case Success(location) =>
Ok(views.html.locations.displayLocation(location))
case Failure(ex) =>
// TODO: Create a prettier error page
InternalServerError("Encountered an error.\n\nDetails: " + ex.toString)
}
}
on line 2 the error is
could not find implicit value for parameter db: globals.ActiveDatabase
not enough arguments for method getLocationById: (implicit db: globals.ActiveDatabase)scala.util.Try[models.Location]. Unspecified value parameter db.
from eatsafe.
What's the full signature of getLocationById
? What does the globals file look like?
I quickly threw together some code on my end and there's no errors. I copied the package object you previously mentioned into a file named Globals.scala
and imported it into the LocationController. The signature of getLocationById
is def getLocationById(locationId: Int)(implicit active: ActiveDatabase): Try[Location]
. And I defined ActiveDatabase
as class ActiveDatabase(val name: String)
.
from eatsafe.
This is now finished
Any methods that work with the database should have (implicit val activeDB: ActiveDatabase)
as a parameter and use that ActiveDatabase to specify which database to connect to via
DB.getConnection(activeDB.name) { conn =>
//use the connection in some way
}
from eatsafe.
Related Issues (20)
- Test admin interface
- Error opening maps in latest Dev HOT 3
- Wrong String displayed for submitting empty city HOT 1
- Test matrix update HOT 1
- Update flow diagram
- Bug: Really long strings give blank page in find location HOT 3
- Bug: multi-map page doesn't change language
- Make translation file entries HOT 2
- Language localization HOT 1
- Remove or Finish Violation [more info] links HOT 3
- Searching for "A & W" does not work very well HOT 1
- Finalize Esperanto messages HOT 4
- Finish Esperanta
- 400 and 404 needs to be accessible in all languages
- Implement logging framework HOT 3
- Pull JavaScript from displayCityMap.scala.html into external file HOT 5
- Fixing Exisiting Browser Tests HOT 3
- Search queries with multiple spaces in a row fail HOT 1
- Admin interface should be able to set coords HOT 1
- Translation to Klingon HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eatsafe.