Injected into Command service is null
Hello.
Please point to my error.
the config is:
`[Inject] public var injector:IInjector;
public function configure():void
{ injector.map(FloxService);
commandMap.map(ConfigEvent.CONFIG_INIT_COMPLETE).toCommand(ShowIntroScreenCommand);
commandMap.map(ScreenEvent.TO_MAIN_SCREEN_REQUEST).toCommand(ShowMainScreenCommand);
commandMap.map(ScreenEvent.TO_INTRO_SCREEN_REQUEST).toCommand(ShowIntroScreenCommand);
commandMap.map(ScreenEvent.SAVE_DATA_REQUEST).toCommand(SaveDataCommand);
mediatorMap.map(IntroScreen).toMediator(IntroScreenMediator);
mediatorMap.map(MainScreen).toMediator(MainScreenMediator);
context.afterInitializing(initComplete);
}
private function initComplete():void
{
dispatcher.dispatchEvent(new ConfigEvent(ConfigEvent.CONFIG_INIT_COMPLETE));
}`
Command
`public class SaveDataCommand extends Command{
[Inject]
public var service:IService;
public function SaveDataCommand() {
trace("service = "+service);
if(service){
service.saveData();
}
else{
trace("Service is NULL");
}
}
} }`
Service
`package dev.div0.services { import com.gamua.flox.Flox;
public class FloxService implements IService {
public function saveGoToMainCount(count:int):void {
Flox.logEvent("go_main_clicked", {count: count});
}
public function saveGoToIntroClick():void {
Flox.logEvent("go_intro_clicked");
}
public function saveData():void {
Flox.getTime(getTimeCompleteHandler, getTimeErrorHandler);
}
private function getTimeErrorHandler(error:String, httpStatus:int):void {
trace("saveData error:"+error +". httpStatus:"+httpStatus);
}
private function getTimeCompleteHandler(time:Date):void {
new SaveData(time.toString());
}
}
}`
in case of service's interface injection
[Inject] public var service:IService;
the error is
Error: Injector is missing a mapping to handle injection into property "service" of object "[object SaveDataCommand]" with type "dev.div0.commands::SaveDataCommand". Target dependency: "dev.div0.services::IService|"
in case of class injection
[Inject] public var service:FloxService;
service is NULL
Comments are currently closed for this discussion. You can start a new one.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
Support Staff 1 Posted by Ondina D.F. on 20 Sep, 2015 12:47 PM
Hi,
injector.map(FloxService); is an incomplete mapping.
The correct mapping would be:
Please read the answers from the following discussions for more details:
http://knowledge.robotlegs.org/discussions/robotlegs-2/8182-the-nec...
http://knowledge.robotlegs.org/discussions/robotlegs-2/5766-automag...
http://knowledge.robotlegs.org/discussions/robotlegs-2/8018-why-i-c...
Also, be aware of the differences between the different mappings :
toValue - Makes the mapping return the given value for each consecutive request.
toType - Makes the mapping return a newly created instance of the given type for each consecutive request.
toSingleton - Makes the mapping return a lazily constructed singleton instance of the mapped type for each consecutive request.
asSingleton - Makes the mapping return a lazily constructed singleton instance of the mapped type for each consecutive request.
asSingleton(initializeImmediately:Boolean=false) - initializeImmediately determines when the instance should be created, immediately after mapping or after the first request
Hope this helps.
Ondina
Support Staff 2 Posted by Ondina D.F. on 20 Sep, 2015 12:50 PM
I forgot to say that if you are injecting an interface, you should map it like this:
3 Posted by dev.div0 on 20 Sep, 2015 01:21 PM
Thank you.
By the way - its my error.
I need to override execute() method to use injected service inside Command - my inattention.
Support Staff 4 Posted by Ondina D.F. on 21 Sep, 2015 03:45 PM
You're welcome!
Concerning the execute method in commands, I didn't notice that you didn't have an execute() in your command. I spotted the incomplete mapping and didn't pay much attention to the code below it.
So yes, injected classes will be null within a constructor, because the Injector hasn't finished its job (injection) yet:
https://github.com/robotlegs/robotlegs-framework/wiki/common-proble...
Just for your information, you don't need to extend the robotlegs Command class. Any class with a public execute method can be used as a Command.
I'm going to mark this discussion as resolved, but you can re-open it, if need be, or you can create a new discussion if you need assistance on another topic.
Ondina
Ondina D.F. closed this discussion on 21 Sep, 2015 03:45 PM.