Skip to content

Commit

Permalink
Component: support bindings for component properties
Browse files Browse the repository at this point in the history
* Closes #26
* Closes #41
* ComponentDef: rename parameter 'map' to 'bind'
  • Loading branch information
jokade committed Apr 11, 2015
1 parent e075347 commit 43dd914
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 23 deletions.
14 changes: 13 additions & 1 deletion src/main/scala/biz/enef/angulate/annotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@ import scala.scalajs.js
@annotation.meta.param
case class named(name: String) extends StaticAnnotation

/**
* Creates a Component definition to be used with the [[Component]] annotation.
*
* @param selector HTML tag name for this component
* @param template
* @param templateUrl
* @param bind
*/
case class ComponentDef(selector: String,
template: String = null,
templateUrl: String = null,
map: js.Dictionary[String] = null)
bind: js.Dictionary[String] = null)

/**
*
* @param cd
*/
case class Component(cd: ComponentDef) extends StaticAnnotation


17 changes: 7 additions & 10 deletions src/main/scala/biz/enef/angulate/impl/ComponentMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ protected[angulate] class ComponentMacros(val c: Context) extends MacroBase with
val ctrlName = ct.typeSymbol.fullName
val defs = cdef - "selector" ++ Map(
"restrict" -> q""""E"""",
"controller" -> q"""${ctrlName}""",
"controllerAs" -> q""""ctrl"""",
"bindToController" -> q"true"
"controller" -> q"""${ctrlName}"""
)
println(defs)

Expand All @@ -41,14 +39,13 @@ protected[angulate] class ComponentMacros(val c: Context) extends MacroBase with

val cm = getConstructor(ct)
val (ctrlDeps,ctrlArgs) = makeArgsList(cm)
val ctrlDepNames = getDINames(cm)
val ctrlDepNames = "$scope" +: getDINames(cm)
val constructor = q"""js.Array[Any](..$ctrlDepNames,
((scope:js.Dynamic, ..$ctrlDeps) => {
val ctrl = new $ct(..$ctrlArgs)
..${copyMembers(ct)}
$debugCtrl
ctrl
}):js.ThisFunction)"""
}):js.Function)"""


val tree =
Expand Down Expand Up @@ -78,9 +75,9 @@ protected[angulate] class ComponentMacros(val c: Context) extends MacroBase with
Map()
}

if(defs.contains("map")) {
val bindings = defs("map")
defs - "map" + ("scope"->bindings)
if(defs.contains("bind")) {
val bindings = defs("bind")
defs - "bind" + ("scope"->bindings)
}
else
defs + ("scope"->q"js.Dictionary()")
Expand All @@ -99,7 +96,7 @@ protected[angulate] class ComponentMacros(val c: Context) extends MacroBase with
"selector" -> defs(names(0)),
"template" -> defs(names(1)),
"templateUrl" -> defs(names(2)),
"map" -> defs(names(3))
"bind" -> defs(names(3))
)
}

Expand Down
13 changes: 7 additions & 6 deletions src/main/scala/biz/enef/angulate/impl/ControllerMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,14 @@ protected[angulate] trait ControllerMacroUtils {


protected def copyMembers(ct: Type) = {
def isGetter(s: MethodSymbol) : Boolean = s.isGetter || s.paramLists.isEmpty
def isSetter(s: MethodSymbol) : Boolean = s.isSetter || s.name.toString.endsWith("_$eq")
def isFunc(s: MethodSymbol) : Boolean = !(isGetter(s) || isSetter(s))

val props = ct.decls.filter( p => p.isPublic && p.isMethod && !p.isConstructor).map( _.asMethod )
val funcs = props.filter( p => !(p.isGetter||p.isSetter) )
val getters = props.filter(_.isGetter)
val setters = props.filter(_.isSetter).map{ s=>
val funcs = props.filter( isFunc )
val getters = props.filter(isGetter)
val setters = props.filter(isSetter).map{ s=>
val name = s.name.toString
val getterName = name.substring(0,name.length-4)
getterName -> s
Expand All @@ -149,9 +153,6 @@ protected[angulate] trait ControllerMacroUtils {
(funcs map { func =>
val funcName = func.name.toString
val (params,args) = makeArgsList(func)
if(func.paramLists.isEmpty)
q"""global.Object.defineProperty(scope,$funcName,literal(get = () => ctrl.$func))"""
else
q"""global.Object.defineProperty(scope,$funcName,literal(value = (..$params) => ctrl.$func(..$args)))"""
})
}
Expand Down
4 changes: 2 additions & 2 deletions tests/src/test/scala/test/ComponentTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ object ComponentTest extends AngulateTestSuite {
'orderedComponentDef- {
module.componentOf[Component1]
val elem1 = compileAndLink("<foo></foo>")
assert(elem1.head.textContent == "component1")
assert(elem1(0).textContent == "component1")
}

'unorderedComponentDef-{
module.componentOf[Component2]
val elem2 = compileAndLink("<foo></foo>")
assert(elem2.head.textContent == "component2" )
assert(elem2(0).textContent == "component2" )
}
}
/*
Expand Down
8 changes: 4 additions & 4 deletions tests/src/test/scala/test/DirectiveTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object DirectiveTest extends AngulateTestSuite {
'directiveOf-{
module.directiveOf[Directive1]
module.directiveOf[Directive1]("anotherDirective")
val $compile = injection[Compile]("$compile")
val $compile = dependency[Compile]("$compile")

val elem1 = $compile("<directive1></directive1>")(literal())(0)
assert( elem1.textContent == "foo" )
Expand All @@ -33,7 +33,7 @@ object DirectiveTest extends AngulateTestSuite {

'postLink-{
module.directiveOf[Directive2]
val $compile = injection[Compile]("$compile")
val $compile = dependency[Compile]("$compile")

val elem = $compile("""<directive2></directive2>""")(literal(bar = "foo"))(0)
js.Dynamic.global.console.log(elem.nodeName)
Expand All @@ -44,8 +44,8 @@ object DirectiveTest extends AngulateTestSuite {

'controllerAs-{
module.directiveOf[Directive3]
val $compile = injection[Compile]("$compile")
val $rootScope = injection[Scope]("$rootScope")
val $compile = dependency[Compile]("$compile")
val $rootScope = dependency[Scope]("$rootScope")

val elem = $compile("""<directive3 x="1"></directive3>""")($rootScope)(0)
$rootScope.$digest()
Expand Down

0 comments on commit 43dd914

Please sign in to comment.