Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

How to pass parameters in config? #40

Open
rabejens opened this issue May 5, 2015 · 3 comments
Open

How to pass parameters in config? #40

rabejens opened this issue May 5, 2015 · 3 comments

Comments

@rabejens
Copy link

rabejens commented May 5, 2015

When using e.g. ionic, you can do something like this in Javascript:

angular.module('mymodule', ['ionic'])
.config(function(someParam1, someParam2) {
  // do something with the parameters
}

How do you do this with scalajs-angular?

@rabejens rabejens changed the title How to pass options in config? How to pass parameters in config? May 5, 2015
@mysticfall
Copy link
Member

Oops, I forgot I had a unanswered question. Sorry about it!

With the latest snapshot, it became possible to use the constructor based DI and it will use a variable's name to resolve the dependency when its type does not have @injectable annotation.

In short, you can just register your Config class normally, like :

module.config[MyConfig]

And then, declare those parameters as constructor arguments of your MyConfig class.

However, I haven't yet tested it myself, so if it does not work please let me know!

@cridmann
Copy link

I think I'm bumping up into this same error. Specifically, I am trying to provide a routing config with a dependency on a Service to use RouteBuilder's resolve method (e.g. - I don't want to instantiate a controller until I fetch some cached data from the server). I am getting angular injection exceptions when I try to do this. Here is some sample code:

class RoutingConfig(routeProvider: RouteProvider, registryService: RegistryService) extends Config {

  routeProvider
    .when("/abcd", RouteBuilder().templateUrl("/assets/templates/home.html").title("Abcd").controller("abcdCtrl").resolve(registryService.resolveDeps).build)
    .otherwise(Route("/assets/templates/404.html"))
}

And here is my registry service:

@injectable("registryService")
class RegistryService(http: HttpService) extends Service {
  require(http != null, "Missing argument 'http'.")

  def getAllServices: Future[Map[String, Map[String, ServiceListing]]] =
    http.get[js.Any]("/api/generic/retrieveGenericServices")
      .map(JSON.stringify(_))
      .map(js => read[Map[String, Map[String, ServiceListing]]](js))

  val futureRegistry = getAllServices

  val resolveDeps: js.Dictionary[js.Any] = js.Dictionary[js.Any]("cachedRegistryData" -> futureRegistry.asInstanceOf[js.Any])

}

@injectable("registryService")
class RegistryServiceFactory(http: HttpService) extends Factory[RegistryService] {
  override def apply(): RegistryService = new RegistryService(http)
}

And here is part of my app main:

    module
      .controller[AbcdCtrl]
      .factory[RegistryServiceFactory]
      .config[RoutingConfig]

@mysticfall
Copy link
Member

@cridmann Could you give me the error message you encountered? And does it occur when you register the modules, or when you do something with them?

Sorry for the late response.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants