12

I'm developing a plugin that adds a new tab to the media modal, and I need to know a way trigger a refresh of the attachments tab so it shows newly added attachments. This is the code I'm using:

wp.media.view.Toolbar.Custom = wp.media.view.Toolbar.extend({
    initialize: function() {
        _.defaults( this.options, {
            event: 'custom_event',
            close: false,
            items: {
                custom_event: {
                    text: wp.media.view.l10n.customButton,
                    style: 'primary',
                    priority: 80,
                    requires: false,
                    click: this.addAttachment
                }
            }
        });

        wp.media.view.Toolbar.prototype.initialize.apply( this, arguments );
    },

    // triggered when the button is clicked
    addAttachment: function(){
        this.controller.state().addAttachment();
        this.controller.setState( 'insert' );
        // I NEED TO TRIGGER A REFRESH OF THE ATTACHMENTS TAB HERE
    }
});

Any help would be appreciated. The media modal documentation is almost non-existant.

Thanks

  • IIRC those are just Backbone/Underscore views. In other words, when you update the model, it should update the view by itself as the "ModelView" should trigger that. – kaiser Feb 25 '15 at 10:01
  • Well, the this.controller.state().addAttachment() function is just an AJAX call using wp.media.post(), so I'd need to trigger an hypothetical "model updated" event somewhere after this AJAX call. Any ideas? – leemon Feb 25 '15 at 10:49
  • "Any ideas?" - currently, no. This is something where I'd have to invest quite some time to read through core (which I don't have now). About your comment: There's MarkDown available (See "help" below "add comment" button). – kaiser Feb 25 '15 at 11:23
0

Try out:

wp.media.editor.get(wpActiveEditor).views._views[".media-frame-content"][0].views._views[""][1].collection.props.set({ignore:(+(new Date()))})

Seems like there must be an easier way but that works for me in the meantime!

A more better way to do it:

wp.media.frame.content.get('gallery').collection.props.set({‌​ignore: (+ new Date())});, 

in this case i'm refreshing the gallery tab.

Try both above codes and see which one works better for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.