-
Notifications
You must be signed in to change notification settings - Fork 42
UIRouter: example how to set controller of a State? #45
Comments
Wow, that was a very detailed analysis, and thanks much for taking time to brought it to my attention. The cause of the problem is that Scala's controller instance itself is not a valid Javascript controller function, so we can't substitute the one with the other, as you already found out. It's just one of the unfortunate limitation of wrapping Angular API with Scala.js. To workaround the problem, It works good for regular named controllers, but it can be problematic for anonymous ones, like used in your example. I still haven't found a perfect solution for this problem, but I managed to workaround for the similar problem with (In Directive.scala) protected def proxy[A <: Controller[ScopeType]]: js.Any = macro ServiceProxy.newClassWrapper[A]
protected def proxy[A <: Controller[ScopeType]](target: A): js.Any = macro ServiceProxy.newObjectWrapper[A] I think a similar approach could be used with the case you mentioned, so I just moved those methods to Please try the latest snapshot and see if it solves the problem. Thanks! |
Thank you Xavier for the response, the explanation and for making the change to
But that would require the named scope trait
it hardly provides any advantage over defining a named controller, in terms of concision and the number of lines-of-code required. In any event, I have since figured out that the
That much eliminated several extra lines of code and avoids mutation of a I'm happy to close this issue now. It may be a while before I learn enough about this library, plus Scala.js, plus AngularUI Routing to be able to apply successfully the solution you have proposed. Again my thanks for your very responsive reply. |
I'm new to this library, so hopefully this is a very easy question. I am using
scalajs-angular
version0.5-SNAPSHOT
andangular-ui-router
version0.2.15
.I am trying to implement the first example on the AngularUI Router tutorial, which is given in JavaScript. I have it working using scalajs-angular except for one part: the setting of the controllers on the state objects.
Update and Tentative Conclusion
I have, if not solved this, at least reached a work-around, posted in detail at the end. In brief, near as I can tell controllers must be registered in order for them to have scope injected, which is not possible with an anonymous function as in the JavaScript version. I could be totally wrong about this, and I invite correction.
End of Update. This Is My Original Questions:
In the JavaScript tutorial it looks like this:
Looking at the companion object for
State
I see the factoryapply()
method has parameters forurl
andtemplateUrl
, both of typeString
, and I set those no problem. I have failed, however, to set thecontroller
member, which theState
trait defines as type js.Any. Since in the JavaScript version, the value ofcontroller
is a function that takes a scope object and sets that object'sitems
property, I tried settingcontroller
to be a Scala function object:With
State1Scope
defined like this:I also tried it with the type of
State1Scope.items
being a ScalaArray
rather thanjs.Array
.I also attempted to set the value of
controller
to be a JavaScript function, like this:Not only did that fail, but I also got a warning that
Members of traits, classes and objects extending js.Any may only contain members that call js.native.
The error I am getting is in my browser console:
I have annotated my
Config
object with@injectable("StateConfig")
.Other Things I Have Tried
Here are some other things I have tried, all unsuccessful:
I thought maybe of setting
controller
to be aController
object like this:But that won't work because
scope
is never defined. (In the JavaScript version, the scope is passed as an argument to the function that is the value of thecontroller
property. The analogous way in Scala would seem to be to makescope
be an argument to theapply()
method of a function object.)Which led me to try this:
That gave me a different error in the browser console:
Error: [ng:areq] Argument 'fn' is not a function, got Object
which sounds as if I'm going in the wrong direction.I also have tried first defining the controller as a separate object. I copied the example
Controller
from from thescalajs-angular
documentation under the heading Property Based Dependency Injection, changing its type annotation ofScope
to myScope
sub-trait, but that failed to compile with the error:I hope I'm missing something really basic. I'll be happy to post more of my code if that will help. I think if I could see a working example that would be enough to get me unstuck. I looked at olivergg's scalajs-ionic-starttabs app but there are no instances of setting the
controller
of aState
in the relevant file. I also looked for any tests in thescalajs-angular
repository that might set thecontroller
member of aState
, but I failed to find any.My sincere thanks and appreciation to anyone who can give me any guidance on this.
End of Original Question
Ultimately I gave up on trying to create the controllers as function literals within invocations of
StateProvider.state()
. Rather I defined them like this:and also registered them in my app module:
and then my invocation of
StateProvider.state
in myStateConfig
looks like:The two arguments to the application of the
View
object are, respectively, the former value of theurlTemplate
argument, and the name of the controller given in the@injectable
annotation.Finally, I had to update the corresponding
ui-view
attributes, setting their values to the keys of theMap
value of theviews
argument toStateProvider.state()
:So this Scala version does what the JavaScript example tutorial does, though it's built a bit differently with named controllers rather than anonymous functions. I am a long way from having a solid understanding of everything going on here, so if anyone feels like shedding light on this situation, I will be eager to learn all I can.
The text was updated successfully, but these errors were encountered: