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 / aspect_example.coffee
Created November 24, 2012 13:56
Coffeescript aspects
class MyClass
foo: ->
alert "foobar"
bar: ->
alert "barfoo"
@foo()
@milankinen
milankinen / gist:4261912
Created December 11, 2012 20:33
Aspect examples
class MyClass
simple: ->
alert "hello!"
another: ->
alert "hi!"
withParams: (a, b) ->
alert a + " - " + b
withReturnValue: (a) ->
return 2 * a
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
import com.tunkkaus._MultiModuleResourceMapper
beans = {
_MultiModuleResourceMapper.enable()
}
# 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) ->
# 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)
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..."
<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>
@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")))
@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)