Interrupt uploading multiple files from view/Mediator

rivella50's Avatar

rivella50

21 Mar, 2013 08:50 AM

Hi,

i've just found this post:
http://knowledge.robotlegs.org/discussions/questions/170-best-pract...

Without a FSM what would now be the best approach to cancel the command which uploads multiple files to a remote server when the user clicks the cancel button ?
Let's say the list of files that need to be uploaded is residing in a model which the Mediator and the Command both know.
Couldn't i just empty that list (or set a Boolean attribute 'isUploading' to false) in Mediator when user clicks the cancel button, i.e. would the empty list be noticed in the running Command ?
Alternatively the same scenario by calling another command which would empty the list of remaining files to upload, would the model change be noticed by the simultaneously running uploading Command ?

Thank you for your thoughts!

  1. Support Staff 1 Posted by Ondina D.F. on 22 Mar, 2013 02:47 PM

    Ondina D.F.'s Avatar

    Hi Valley,

    You can try using commandMap.detain and commandMap.release.

    Here a simple scenario: in your LoadSomethingCommand:

    override public function execute():void
    {
                commandMap.detain(this);
                model.doSomething("BLAH");//sets someProperty
                model.callBack=callback;
    }
    
    private function callback():void
    {
                commandMap.release(this);
    }
    

    In your Model:

    public var callBack:Function;
    …
    public function doSomething(value:String):void
    {
                someProperty=value;
                if(someProperty=="")
                    callBack.call();
    }
    

    When user clicks cancel, the view dispatches an event, the mediator can re-dispatch it to trigger a command that sets someProperty to null or to whatever you needed it to be, and the Model will call command’s callBack method, where you release the command.

    But I’m not sure whether that’s solving your problem entirely. You’ll have to make sure that the changes to your Model’s data won’t affect the state of your app in an undesired way after cancelling an async process.

    Maybe you can also take a look at:
    Tim Oxley’s Undoable Command Classes for Robotlegs Framework:
    https://github.com/timoxley/robotlegs-utilities-UndoableCommand

    and this fork:
    https://github.com/d3zza/robotlegs-utilities-UndoableCommand

  2. 2 Posted by rivella50 on 22 Mar, 2013 11:18 PM

    rivella50's Avatar

    Hi Ondina,

    so in terms of my uploading task: The called uploading command sees a list of n files to upload to a server async in model myModel. It starts with the first and registers for progress and complete events. Whenever one of these events will be received the command checks in myModel about changes.
    Anytime the user can click a cancel button. If i understand you correctly the Mediator then calls a 2nd command CancelUploadCommand, which sets a flag 'isCancelled' to true in the same model myModel and clears the list of remaining files to upload.
    Will the 1st command (which is still running and uploading) see these two changes which have been performed by the 2nd command and therefore cancel all future uploading steps ?
    To keep the 1st command running i would definitely use

    commandMap.detain(this);
    

    I don't think i need callbacks for that.

  3. Support Staff 3 Posted by Ondina D.F. on 23 Mar, 2013 12:20 PM

    Ondina D.F.'s Avatar

    Hi Valley,

    Anytime the user can click a cancel button. If i understand you correctly the Mediator then calls a 2nd command CancelUploadCommand, which sets a flag 'isCancelled' to true in the same model myModel and clears the list of remaining files to upload. Will the 1st command (which is still running and uploading) see these two changes which have been performed by the 2nd command and therefore cancel all future uploading steps ?

    Right. Since the uploading command is being detained, it will notice the changes in the model.

    Of course you can set the model’s flag ‘isCancelled’ inside your mediator as well, if you really wanted or needed to. I’d prefer letting a command access the model. The reasons for that are already well explained in a number of discussions on this forum. If you’re interested in knowing why (most of the time) is better to not inject a model into a mediator, search the discussions for “inject model into mediator”.

    I don't think i need callbacks for that.

    Yes, you’re right; you don’t need a callback method. In my little example I just wanted to show you how to use detain and release. You know your use case better than me, so you’ll decide what you need to do :) Anyway, you’ll have to call commandMap.release(this); when isCancelled==true, so the command can get garbage collected.

    Did I answer all your questions?

    If you think your problem has been resolved, you can close this discussion, or let me know so I can close it myself. And, of course, you can re-open it, if you have any further questions regarding this issue.

    Cheers,
    Ondina

  4. 4 Posted by rivella50 on 28 Mar, 2013 04:40 PM

    rivella50's Avatar

    Hi Ondina,

    it works fine now: I have extracted the uploading code to a Service.
    There is one command that starts the upload and another that interrupts it (closing the running URLLoader instance in the Service) if the user wants that.
    Therefore no Boolean flag anymore in another model needed.
    Please close the discussion.
    Thanks again for your help.
    valley

  5. Support Staff 5 Posted by Ondina D.F. on 28 Mar, 2013 05:12 PM

    Ondina D.F.'s Avatar

    Hey Valley,
    No problem. I’m glad it worked out for you.

  6. Ondina D.F. closed this discussion on 28 Mar, 2013 05:12 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