Skip to content

Instantly share code, notes, and snippets.

View dsugden's full-sized avatar

Dave Sugden dsugden

  • Shopify
  • Gatineau
View GitHub Profile
@dsugden
dsugden / atheleteForm.js
Created April 10, 2012 22:58
Example backbone view using the require.js API for dependencies
define([
'jquery',
'underscore',
'backbone',
'text!templates/athelete/atheleteForm.html'
'i18n!nls/atheleteLabel'
], function($, _, Backbone, atheleteForm, atheleteLabel ){
var AtheleteForm = Backbone.View.extend({
el: "#athleteForm",
@dsugden
dsugden / atheleteLabel.js
Created April 29, 2012 22:22
i1n8 with require and backbone
//Contents of js/nls/atheleteLabel.js
define({
'root': {
NAME : 'Name',
...
},
'fr-fr': true
});
//Contents of js/nls/fr/atheleteLabel.js
define({
NAME : 'Nom',
...
});
@dsugden
dsugden / gist:2553568
Created April 29, 2012 22:28
Play! controller method for backbone.js JSON create
/**
* Service Backbone save();
* @param id
*/
public void create(){
JSONDeserializer<Athelete>reportDeserializer = new JSONDeserializer<Athelete>().use( null, Athelete.class );
String json = params.get("body");
Athelete athelete = reportDeserializer.deserialize(json);
athelete.save();
@dsugden
dsugden / Articles.scala
Last active December 11, 2015 04:18
Articles Slick object
import play.api.db._
import play.api.Play.current
import java.sql.Date
import scala.slick.driver.PostgresDriver.simple._
import play.Logger
import scalaz.Failure
import scala.slick.lifted.Projection
import Users._
import scala.slick.lifted.BaseTypeMapper
import scala.slick.driver.BasicProfile
case class Article(id: Option[Long] = None,
name: String,
body: Option[String], // this is the body in edit
userID: Long,
created: Date,
category: String,
published: Boolean = false,
displayOrder: Int = -1,
publishedBody: Option[String] // body as published
)
@dsugden
dsugden / ArticleId.scala
Created January 21, 2013 00:38
Alternative representations of Article
case class ArticleId(id: Long)
case class ArticleForDisplay(id: Option[Long] = None,
publishedBody: Option[String],
userID: Long,
created: Date,
category: String,
published: Boolean)
case class ArticleForCreation(name: String, body: Option[String], category: ArticleCategory, published: Boolean = false)
@dsugden
dsugden / Example_calling_update.scala
Last active December 11, 2015 09:49
Example of JSON Read[A] and Try[A]
Articles.update(valid) match {
case Success(a) => {
cache.Cache.remove("blog")
Ok(Json.toJson(ArticleId(valid.id)))
}
case Failure(e) => {
Logger.error("update error:", e)
BadRequest(Json.toJson(Map("error" -> e.getMessage())))
}
}
val ArticleTable = new Table[Article]("Article") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def body = column[Option[String]]("body")
def publishedBody = column[Option[String]]("publishedBody")
def userID = column[Long]("user_Id")
def created = column[Date]("created")
def category = column[String]("category")
def published = column[Boolean]("published")
def displayOrder = column[Int]("displayOrder")
@dsugden
dsugden / ArticleCreate.scala
Created January 21, 2013 00:55
Create an article
def create(userId: Long, articleForCreation: ArticleForCreation): Try[ArticleId] = database.withSession { implicit db: Session =>
Try(
ArticleId(
ArticleTable.forInsert.insert(
Article(None,
articleForCreation.name,
articleForCreation.body,
userId,
new java.sql.Date(System.currentTimeMillis()),
articleForCreation.category.toString(),