Im wondering how Injections works, preps, best practises?

Christen's Avatar

Christen

14 Jul, 2011 12:06 PM

Hi

When/how can you use an injected class?

If I have a class that is not set by the context, can it use injected classes?
Example view1 has a sub class view2 and view 2 is not set in the context, when I try to get the model from the [Inject] model:Model; the model i null.

If this is correct all classes that wants to use ie. a sigelton model class has to be injected them self?

What if you have a class that you want to create and destroy from time to time (not a sigelton). How do you do this?

  1. 1 Posted by Christen on 14 Jul, 2011 12:17 PM

    Christen's Avatar

    Ok this is a stupid question. Of cause I cannot use the framework if I am not connected to it.

    But I am still wondering how do I have a class that I create and destroy, but is still in contact with the framework (ie. model)? Do I pass reference to it, or can I do it some other way?

    I don't want to make all my classes sigeltons or mediated views. IE. a service that I only juse once to grab some info.

  2. 2 Posted by Stray on 14 Jul, 2011 12:21 PM

    Stray's Avatar

    Hi Christen, in a command you can do:

    injector.instantiate(SomeModelClass)

    and it will instantly give you an instance of the SomeModelClass, with the injections completed.

    Does this help?

    Stray

  3. 3 Posted by Christen on 14 Jul, 2011 12:32 PM

    Christen's Avatar

    Is the class is ready to use the other classes injected in the context?

    I have a cookieservice that gets userinfo from a cookie, but I only need to use it once. So in my PrepServiceCommand Controller i can write injector.instantiate(CookieService) and then I can get the model from CookieService when I use it? And still remove it without it thaking up memory?

    Which leeds me to the next question. Where would you trigger a CookieService from? The prep command? Or is this a bad practice.

    I have not fully comprehended all the consepts behind robotlegs and Signals, I'm still trying to get it into my head. :)

  4. 4 Posted by Stray on 14 Jul, 2011 12:38 PM

    Stray's Avatar

    Hi Christen,

    that would work.

    You'll need to use

    commandMap.detain(this)

    and

    commandMap.release(this)

    in your Command to keep everything alive while your service is busy. Then, when the model is sorted you can just release() the Command and then the service will be garbage collected.

    However... people don't usually worry about the memory taken up by a service... are you building this for a digital watch or something? ;)

    Stray

  5. 5 Posted by Christen on 14 Jul, 2011 12:48 PM

    Christen's Avatar

    He he. :)

    No, Im not building it for a low performance device.
    Im just trying to figure out best practices. Trying to do it right instead of just dooing it, I guess. Bad idea.

    Thanks for you help Stray

    How about where would you trigger a CookieService from? The prep command? Or is this a bad practice.
    Usualy triggers come from a view. But maybe it is a bit wird?
    Loginview -> autoLoginSignal -> autoLoginCommand -> cookieService -> model -> Signal -> Loginview.

  6. 6 Posted by Christen on 14 Jul, 2011 12:59 PM

    Christen's Avatar

    Hmm Im getting an error:

    Injector is missing a rule to handle injection into target [object CookieService]. Target dependency: model::Settings

  7. 7 Posted by Stray on 14 Jul, 2011 01:02 PM

    Stray's Avatar

    Hi Christen - check out the common problems wiki - there is a link on the top of the right side of the menu on the front of the forum.

    Stray

  8. 8 Posted by Christen on 14 Jul, 2011 01:11 PM

    Christen's Avatar

    Thanks, will do.

  9. Ondina D.F. closed this discussion on 02 Nov, 2011 05:36 PM.

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