Skip to content

Instantly share code, notes, and snippets.

View milankinen's full-sized avatar
🐦
tsers

Matti Lankinen milankinen

🐦
tsers
View GitHub Profile
@milankinen
milankinen / OrderingDSL.scala
Last active December 26, 2015 00:09
Scala ordering DSL
object OrderingDSL {
case class AscendingOrder[T](value: T)(implicit ord: Ordering[T]) extends Ordered[AscendingOrder[T]] {
def compare(that: AscendingOrder[T]): Int = ord.compare(value, that.value)
}
case class DescendingOrder[T](value: T)(implicit ord: Ordering[T]) extends Ordered[DescendingOrder[T]] {
def compare(that: DescendingOrder[T]): Int = -ord.compare(value, that.value)
}
def asc[T](value: T)(implicit ord: Ordering[T]) = AscendingOrder(value)
@milankinen
milankinen / OrderingDSLExample.scala
Last active December 26, 2015 00:09
Scala ordering DSL example
object OrderingDSLExample extends App {
case class My(num: Int, str: String)
val myValues = List(My(3, "foo"), My(3, "bar"), My(5, "bar"), My(5, "foo"))
import OrderingDSL._
require(myValues.sortBy(m => (asc(m.num), asc(m.str))) ==
List(My(3, "bar"), My(3, "foo"), My(5, "bar"), My(5, "foo")))
require(myValues.sortBy(m => (asc(m.num), desc(m.str))) ==
List(My(3, "foo"), My(3, "bar"), My(5, "foo"), My(5, "bar")))
<script type="text/x-template" id="person-form-template">
<div class="form-horizontal">
<div class="control-group">
<label class="control-label" for="name">Name</label>
<div class="controls"><input class="input" type="text" name="name" id="name" value="{{name}}" /></div>
</div>
<div class="control-group">
<label class="control-label" for="age">Age</label>
<div class="controls"><input class="input" type="number" name="age" id="age" value="{{age}}" /></div>
</div>
jQuery ->
# Simple person model with two attributes
class Person extends Backbone.Model
validation:
age:
required: true
min: 10
name:
required: true
msg: "All persons have a name..."
# PART 2: bind our backbone view to validation events and also for
# input changes so that model changes automatically when input changes
# =======================================================================
# This defines which element's attribute will be used for linking
inputSelector = 'name'
# model update function, which is called when input changes
updateModel = (model, attr, value) ->
model.set(attr, value || null)
# PART 1: tweak backbone validation a little bit
# ===============================================
# first, allow errorous inputs to update our model
Backbone.Validation.configure
forceUpdate: true
# then disable default callbacks for backbone validation events
_.extend Backbone.Validation.callbacks,
valid: (view, attr, selector) ->
import com.tunkkaus._MultiModuleResourceMapper
beans = {
_MultiModuleResourceMapper.enable()
}
package com.tunkkaus
import org.grails.plugin.resource.ResourceMeta
import org.grails.plugin.resource.ResourceProcessor
import org.grails.plugin.resource.mapper.MapperPhase;
import org.grails.plugin.resource.module.ModuleBuilder
import org.springframework.util.AntPathMatcher
/**
* We are using prefix '_' because we want that this resource mapper
@milankinen
milankinen / tiny-cycle-3.js
Created December 10, 2015 20:08
tiny-cycle-3.js
let i = 0
let main = () => document.querySelector("#app").innerHTML = `Seconds elapsed ${i++}`
main() && setInterval(main, 1000)
@milankinen
milankinen / ServerOnly.js
Created December 9, 2015 08:33
React "ServerOnly" component
import React from "react"
import {findDOMNode} from "react-dom"
export default React.createClass({
componentWillMount() {
if (process.browser) {
this.setState({html: findDOMNode(this).innerHTML})
}
},
render() {