0

I have two ViewController. The first one fires an event if I select an item in a treepanel :

// treeController

    onItemSelection: function(treeview, record) {
        var me = this,
            controller = me.getView().getController();

        ...

        controller.fireEvent('myEvent', record);
    }

The second one is listening to this event. The controller is responsible for uploading a file to a specified url. This url is set by the onMyEvent-function.

// uploadController

    ...    
    listen: {
            controller: {
                '*': {
                    myEvent: 'onMyEvent'
                }
            }
        },

        defaultUrl: 'foo/bar/{id}',
        currentUrl: null,

        onMyEvent: function(record) {
            var me = this;
            me.currentUrl = me.defaultUrl.replace('{id}', record.getId());
        },

        onUploadClick: function (form) {
            var me = this;
            if (form.isValid()) {
                form.submit({
                    url: me.currentUrl,
                    ...
                });
            }
        },
    ...

What I want to achieve: I select an item in the treepanel -> the event is fired, the onMyEvent-function has been executed.

I click on the button to open the uploadView (the view which is connected to the controller). After that I'll click on the fileupload-button, select a file and click on the uploadbutton. After the uploadbutton has been pressed, the controller should call the onUploadClick-function and use the previous placed url (currentUrl) for the upload.

The problems I'm facing:

  1. Selecting an item in the treepanel fires the event, but the uploadController is not executing the onMyEvent-function. When I open the uploadView first and select afterwards a node in the panel, the onMyEvent-function is executed.

  2. When I use the second approach and try to upload the file, I get an error which tells me I haven't specifed the url (its null).

How can I accomplish the process without using the mentioned workaround for 1.?

Thanks in advance.

xhadon
  • 876
  • 14
  • 33
  • I agree with @JuanMendes that it will be better to fire the events from View instead of Controller. Moreover, I was wondering why do you need another event to set the Url when it could be just a function? If you're going to execute it from multiple sources, then it's acceptable. However, in that case I'd define it in the parent ViewController. – Dumbledore Oct 22 '15 at 14:11
  • 1
    My suggestion, don't fire events from controllers. Views fire events and controllers listen to events. – Ruan Mendes Oct 22 '15 at 14:25
  • If these two components have to work together why don't you put them on a new view with its own viewController? – Mattex83 Oct 22 '15 at 14:34
  • Seems to me that you have a problem in your architecture. I'm not sure about controllers should not fire events, I think controllers can perfectly fire events for other (child)controllers to listen to, but in your case I think you should redesign your app.. – Tarabass Oct 22 '15 at 17:28
  • I found this kind of approach on different websites (and blogs / articles of sencha developer) and the sencha documentation. I would really appreciate if you could give me a valuable answer instead of telling me how it should not be done (because this isn't helping so far). – xhadon Oct 23 '15 at 07:46

1 Answers1

1

Your event myEvent is in UploadController. However, controller.fireEvent('myEvent', record); would try to find and fire it in TreeController.

Root cause of this issue is that controller = me.getView().getController(); is going to give you back this/instance of TreeController. When you do me.getView(),it gives you TreeView and me.getView().getController() is going to give you back an instance of TreeController and you need an instance of UploadController cause myEvent is an event of UploadController.

The reason you're able to fire the event when you open UploadView first is cause you're already in UploadController.

Hope that helps!

Dumbledore
  • 450
  • 1
  • 5
  • 20