Created
June 3, 2014 09:38
-
-
Save michaelstephendavies/983790a7ecd49a29981f to your computer and use it in GitHub Desktop.
meteor.sjs
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
// USAGE: | |
// define a method. Can optionally add a Match pattern to each argument. | |
method add(Number arg1, Number arg2, uncheckedArg) { | |
return arg1 + arg2; | |
} | |
/* | |
Meteor.methods({ | |
add: function (arg1, arg2, uncheckedArg) { | |
check(arg1, Number); | |
check(arg2, Number); | |
return arg1 + arg2; | |
} | |
}); | |
*/ | |
// define a publication. Can optionally add a Match pattern to each argument. | |
publication things(String str, unchecked) { | |
return Things.find({owner: this.userId, something: str}); | |
} | |
/* | |
Meteor.publish('things', function (str, unchecked) { | |
check(str, String); | |
return Things.find({owner: this.userId, something: str}); | |
}); | |
*/ | |
// define template helpers and events | |
template thing { | |
rendered() { | |
console.log("was rendered"); | |
} | |
addIncrement(x) { | |
return x + this.increment; | |
} | |
blahOfSomething() { | |
return blah(this.something); | |
} | |
on "click .thing" { | |
console.log("clicked the thing"); | |
} | |
on "keydown input" (evt) { | |
Session.set("blah", $(evt.currentTarget).val()); | |
} | |
} | |
/* | |
Template.thing.rendered = function () { | |
console.log('was rendered'); | |
}; | |
Template.thing.addIncrement = function (x) { | |
return x + this.increment; | |
}; | |
Template.thing.blahTheSomething = function () { | |
return blah(this.something); | |
}; | |
Template.thing.events({ | |
'click .thing': function () { | |
console.log('clicked the thing'); | |
} | |
}); | |
Template.thing.events({ | |
'keydown input': function (evt) { | |
Session.set('blah', $(evt.currentTarget).val()); | |
} | |
}); | |
*/ | |
// THE MACROS | |
// used by method and publication | |
macroclass argType { | |
pattern {$type:expr $name:ident} | |
pattern {$name:ident} where ($type = #{}) // why does this work? | |
} | |
macro method { | |
rule {$name:ident ($args:argType (,) ...) {$code ...}} => { | |
Meteor.methods({ | |
$name: function($args$name (,) ...) { | |
$(check($args$name, $args$type);) ... | |
$code ... | |
} | |
}); | |
} | |
} | |
// used by publication | |
macro __stringifyIdent { | |
case {_ $x:ident} => { | |
var pattern = #{$x}; | |
var tokenString = pattern[0].token.value.toString(); | |
var stringValue = makeValue(tokenString, #{$here}); | |
return withSyntax($val = [stringValue]) { | |
return #{$val}; | |
} | |
} | |
} | |
macro publication { | |
rule {$name:ident ($args:argType (,) ...) {$code ...}} => { | |
Meteor.publish(__stringifyIdent $name, function ($args$name (,) ...) { | |
$(check($args$name, $args$type);) ... | |
$code ... | |
}); | |
} | |
} | |
macro template { | |
rule {$name {}} => {} | |
rule { | |
$name { | |
$helperName($args:ident (,) ...) {$code ...} | |
$rest ... | |
} | |
} => { | |
Template.$name.$helperName = function ($args (,) ...) { | |
$code ... | |
}; | |
template $name {$rest ...} | |
} | |
rule { | |
$name { | |
on $evt:lit ($args:ident (,) ...) {$code ...} | |
$rest ... | |
} | |
} => { | |
Template.$name.events({ | |
$evt: function ($args (,) ...) {$code ...} | |
}); | |
template $name {$rest ...} | |
} | |
rule { | |
$name { | |
on $evt:lit {$code ...} | |
$rest ... | |
} | |
} => { | |
Template.$name.events({ | |
$evt: function () {$code ...} | |
}); | |
template $name {$rest ...} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What atmosphere package are you using to compile those macros ?