Model Issue

oscar's Avatar


10 May, 2010 09:15 AM

I have a view component that is added to the stage at tun time several times.

In my contact I have:

mediatorMap.mapView( myView, myViewMediator );
injector.mapClass( myViewModel, myViewModel );

I'm trying to dispatch an event from the view that is listened by the mediator, the mediator shoul dispatch another event that is listd by a command, and the command should update the view. The problem is that I can not get to the correct model.

When my models are created, in the constructor Y have:

public var internalId:Number;
internalId = Math.random()*1000;

everytime a view dipatches an event, it's received by a model with a different view. Looks like with every event, a new model is created.

any ideas?

  1. 1 Posted by oscar on 10 May, 2010 09:17 AM

    oscar's Avatar

    I wanted to mention that my views are added to a component that is not mediated.

  2. 2 Posted by Jason Dias on 10 May, 2010 09:24 AM

    Jason Dias's Avatar

    If you only want one model you should use `injector.mapSingleton(
    myViewModel );`

  3. 3 Posted by oscar on 10 May, 2010 09:25 AM

    oscar's Avatar

    No, I have several instances of the view component and I need several models, one for each view. But I need to be able to acess the correct model, when the mediator dispatches events.

    tks for your response.

  4. Support Staff 4 Posted by Shaun Smith on 10 May, 2010 09:29 AM

    Shaun Smith's Avatar

    mapClass() supplies a new instance for every request, so your command is being injected with a new model instance every time. If you want the command to act on a particular model, you'll need to pass a reference to that model along with the event.

  5. 5 Posted by oscar on 10 May, 2010 09:34 AM

    oscar's Avatar

    can you please give me an example? I'm not sure I got it.

  6. Support Staff 6 Posted by Shaun Smith on 10 May, 2010 09:55 AM

    Shaun Smith's Avatar

    If I understand you correctly, each sub view has its own model, and you want to dispatch an event from such a view that gets caught by a mediator, relayed to the framework, and have a command update that model.

    So, let's say each sub view has a "model" property, and it dispatches a bubbling event ViewEvent.TRIGGER. The mediator could catch this event:

        // In onRegister - ViewEvent must be set to bubble if the component that dispatches the event is nested.
        eventMap.mapListener(view, ViewEvent.TRIGGER, handleTrigger, ViewEvent);

    You would then create an application event that has a reference to the correct model:

        // In handleTrigger
        var myView:MyView = as MyView;
        dispatch(new AppEvent(AppEvent.TRIGGER, myView.model));

    The command would be bound to AppEvent.TRIGGER.

    ViewEvent, AppEvent and TRIGGER are bad names, but I'd need to know more about the application to give them better ones. Anyhow, the idea is that you dispatch a custom Event that has a reference to the model that you want to update.

  7. 7 Posted by oscar on 10 May, 2010 10:05 AM

    oscar's Avatar

    Shaun, thanks for your response.

    This is very similar to what I have. I'm not sure if you meant that I should insert (a reference) the model into the view, I thought it but couldn't do it.

    My problem right now is that after dispatching AppEvent.TRIGGER, the command that is support to work on the model doesn't get the correct model. In fact, it gets a new model.

  8. Support Staff 8 Posted by Shaun Smith on 10 May, 2010 10:11 AM

    Shaun Smith's Avatar

    The command will be injected with the AppEvent (which will have a reference to the model). You can't inject model directly into the command as the injector won't know which model to inject.

  9. 9 Posted by oscar on 10 May, 2010 10:14 AM

    oscar's Avatar

    ok, but how do I inject the correct model into the view? when?

  10. Support Staff 10 Posted by Shaun Smith on 10 May, 2010 10:22 AM

    Shaun Smith's Avatar

    Well, I tend to avoid injecting anything into my views. Instead, I would give each view a unique ID, and I would have a singleton Model that contains a collection of models that can be retrieved by ID. That way, the models stay out of my views, and I have a single collection that can be injected anywhere in my application. But it really depends what you are building.

    What does your view-model contain? How are the views being created?

  11. 11 Posted by jiri on 10 May, 2010 10:30 AM

    jiri's Avatar

    Would it not be possible to create a Injector.child() that is available in the new version of Robotlegs? The newly create child injector needs to receive a rule about the Model so that you can inject the Model in the Command.
    Dont know if this is possible, because I am quite new to RL, but it is just an idea.


  12. oscar closed this discussion on 24 Jul, 2010 04:00 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? 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