Announcement

Collapse
No announcement yet.

Capturing after:save event of bottom panel link from field view

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Capturing after:save event of bottom panel link from field view

    Hello,

    Scenario

    Say we have a bottom panel in detail view of Entity A. I click the edit drop-down of one of the records in the bottom panel to open the edit modal dialog window. I edit the record and click save. This closes the dialog and shows the detail view of Entity A again.

    What I'm trying to achieve

    I have a field in the detail view of Entity A. I want this field to be able to listen to the after:save event described above so I can tell it to re-render itself if any of the bottom panel records are edited and saved.

    What I've tried

    I tried adding this to a custom view of my field. But this only fires if I edit entity A's fields or click edit and then save on the detail screen. This doesn't fire when editing bottom panel records in their corresponding dialog windows.

    Code:
    setup: function () {
    
    
    Dep.prototype.setup.call(this);
    
    this.listenTo(this.model,'after:save',(model,options) => {
    console.log('afterSave');
    });
    
    },​

  • #2
    I think it can be done from the relationship panel view. You can define a custom view in clientDefs > relationshipPanels. There can have access to both the parent model, and the list view collection. Not sure, but likely there will be some event called on the collection. You can check which one by subscribing to the 'all' event.

    Comment


    • czcpf
      czcpf commented
      Editing a comment
      Thank you so much! I will check it out.

  • #3
    Since I needed my event to fire anytime any of the bottom panel collections changed, I decided to use a dynamic-handler instead. Below is my code for that for anyone else needing something like this.


    Code:
    define('mymodule:my-dynamic-handler', 'dynamic-handler', function(Dep) {
    
        return Dep.extend({
    
            // called on initialization
            init: function() {
    
                this.recordView.listenTo(
                    this.recordView,
                    'after:render',
                    this.afterRender.bind(this)
                );
    
                //standard listener for model changes
                this.recordView.listenTo(this.model, 'after:save', (model, options) => {
                    this.refreshMyModel();
                });
            },
    
            // put things that depending on rendering inside here
            afterRender: function() {
                this.setupBottomPanelCollectionHandlers();
            },
    
            //setup listen events for all the collections
            setupBottomPanelCollectionHandlers: function() {
    
                let bottomPanelCollections = this.getBottomPanelCollections();
                for (let key in bottomPanelCollections) {
                    let collection = bottomPanelCollections[key];
    
                    //add handlers for collection changes (added, remove, changes)
    
                    //listen to the sync event once to determine when the collection is finished loading initially.
                    //otherwise, 'update' event will fire when collection is finished loading
                    this.recordView.listenToOnce(collection, 'sync', (collection, data, obj) => {
                        // event fires when models have been added, removed, changed (this includes new models which have not been saved to db, ie new rows)
                        // curiously, this event doesn't fire for existing models when you change their individual fields. for that we need the change event below
                        this.recordView.listenTo(collection, 'update', (collection, o) => {
                            this.refreshMyModel();
                        });
                        //event fires anytime something in the collection changes (this seems to include existing models only)
                        this.recordView.listenTo(collection, 'change', (model, o) => {
                            this.refreshMyModel();
                        });
    
                    });
                }
    
            },
    
            /**
             * Return a object of collections in the bottom panels
             * @return {Object<Collection>|{}}
             */
            getBottomPanelCollections: function() {
    
                let bottomPanelCollections = {};
                let bottomView = this.recordView.getView('bottom') || null;
    
                if (!bottomView) {
                    return null;
                }
    
                let bottomPanelViews = bottomView.nestedViews;
    
                for (let key in bottomPanelViews) {
                    //only add bottom panel views that have collection
                    if (!bottomPanelViews[key].collection) {
                        continue;
                    }
                    bottomPanelCollections[key] = bottomPanelViews[key].collection;
                }
    
                return bottomPanelCollections;
    
            },
    
            //this function is called when collections are saved, changed, or removed or when the model itself changes
            refreshMyModel: function() {
    
                //refresh model to update all fields
                this.model.fetch();
    
            },
    
    
        });
    });​
    ​


    Comment

    Working...
    X