This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
176 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,36 @@ | |||
Backend: form with meta | |||
======================= | |||
|
|||
In some cases you will find it useful to send some payload with the form that you should be able to extract before processing | |||
the form with object. | |||
|
|||
One good example is sending an entity id of the object backing the form, so that on each POST you will be able to pull | |||
it from the database and apply changes that came from the frontend. | |||
|
|||
Add meta on form creation | |||
------------------------- | |||
|
|||
When creating the form use the withMeta syntax:: | |||
|
|||
case class Person(id: String, name: String) | |||
|
|||
val personForm = form[Person](f => List( | |||
f.field(_.name).label("Name") | |||
)) | |||
|
|||
personForm(person).withMeta("id", person.id).generateJSON | |||
|
|||
|
|||
which will add the needed payload to the form. | |||
|
|||
Extract the meta on form posts | |||
------------------------------ | |||
|
|||
The meta can be later extracted on POST calls with:: | |||
|
|||
val id = personForm.getMeta(jsonBody)("id") | |||
val person = personDao.lookup(id) | |||
personForm(person).process(jsonBody).generateJSON | |||
|
|||
|
|||
where jsonBody is the serialized object that comes from the frontend. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,32 @@ | |||
package org.supler | |||
|
|||
import org.json4s.JValue | |||
import org.json4s.JsonAST.{JField, JObject, JString} | |||
|
|||
case class FormMeta(meta: Map[String, String]) { | |||
|
|||
def apply(key: String): String = { | |||
meta(key) | |||
} | |||
|
|||
def +(key: String, value: String) = this.copy(meta = meta + (key -> value)) | |||
|
|||
def toJSON = JField(FormMeta.JsonMetaKey, JObject(meta.toList.map {case (key, value) => JField(key, JString(value))})) | |||
|
|||
def isEmpty = meta.isEmpty | |||
} | |||
|
|||
object FormMeta { | |||
val JsonMetaKey = "supler_meta" | |||
|
|||
def fromJSON(json: JValue) = { | |||
json match { | |||
case JObject(fields) => fields.toMap.get(JsonMetaKey) match { | |||
case Some(JObject(entries)) => FormMeta(entries.toMap.collect{case (key: String, value: JString) => key -> value.s}) | |||
case Some(_) => throw new IllegalArgumentException("Form meta is not well formed") | |||
case None => FormMeta(Map()) | |||
} | |||
case _ => FormMeta(Map()) | |||
} | |||
} | |||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.