minject, mapValue not working at all for named DivElement

32 views
Skip to first unread message

Jason Kringen

unread,
Mar 26, 2014, 3:15:32 PM3/26/14
to haxe...@googlegroups.com
Hi, hopefully someone can shed some light on this for me. I've looked at the minject documentation quite a bit on this and I think I'm doing this exactly the way I'm supposed to, but this just doesn't work at all.
I have a class that contains 3 different js.html.DivElement member variables that I want to inject using mapValue(), but when I try this all I get is this error: "Uncaught TypeError: Cannot call method 'join' of undefined"

Here the class I want to inject into:

class TvDisplay {
   
@inject public var model:TvModel;
    @inject("osd") public var osdElement:DivElement;
   
@inject("message") public var messageDialog:DivElement;
   
@inject("loading") public var loadingScreen:DivElement;

   
public function new() {
   
}

   
@post
   
public function initialize():Void {
        trace
('osdElement=$osdElement');
   
}
}

Here is my injection setup code:

var injector:Injector = new Injector();
injector
.mapSingleton(TvModel);

var osdElement:DivElement = cast Browser.document.getElementById("osd");
var messageElement:DivElement = cast Browser.document.getElementById("messageDialog");
var loadingElement:DivElement = cast Browser.document.getElementById("loadingScreen");
injector
.mapValue(DivElement, osdElement, "osd");
injector
.mapValue(DivElement, messageElement, "message");
injector
.mapValue(DivElement, loadingElement, "loading");

var instance:Bootstrap = injector.instantiate(Bootstrap);

The Bootstrap class I haven't included here, but it has a class member that is of type TvDisplay and is being injected properly. But whenever I try and use the mapValue() methods to map those DivElements, it never works at all and spits out that error I listed above.

I am able to get this all to work by manually creating an instance of TvDisplay, manually setting the 3 element field values, then using mapValue() to map the TvDisplay class, but I'd rather use named mappings that the documentation says is possible. Here is that code:

var display:TvDisplay = new TvDisplay();
display
.osdElement = cast Browser.document.getElementById("osd");
display
.messageElement = cast Browser.document.getElementById("messageDialog");
display
.loadingScreen = cast Browser.document.getElementById("loadingScreen");
injector
.mapValue(TvDisplay, display);

So that works fine, but again, I'd really rather use the named mappings to inject those DivElement objects. According to the documentation, it looks like this should work. Does mapValue() just not work with DOM elements or something? Thanks for any help. 

-Jason

Jason O'Neil

unread,
Mar 26, 2014, 6:17:15 PM3/26/14
to haxe...@googlegroups.com
Hi

I would guess minject only works with classes that work with "Type.getClassName()" and "Type.resolveClass()".

A quick test shows that DOM elements don't work with the Type API:

class Test {
  static function main(){
    try {
          js.Lib.alert( Type.resolveClass("haxe.ds.StringMap") ); // works
          js.Lib.alert( Type.getClassName(haxe.ds.StringMap) ); // works
          js.Lib.alert( Type.resolveClass("js.html.DivElement") ); // null
          js.Lib.alert( Type.getClassName(js.html.DivElement) ); // TypeError: a is undefined
    }
    catch ( e:Dynamic ) js.Lib.alert( e );
  }
}

http://try.haxe.org/#53dA4

The documentation for Type does not mention these limitations, so perhaps this should work?  I'm not sure whether or not there's a workaround...

Jason


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages