如何在媒体模式中触发刷新

时间:2015-02-25 作者:leemon

我正在开发一个插件,在媒体模式中添加一个新选项卡,我需要知道如何触发附件选项卡的刷新,以便显示新添加的附件。这是我正在使用的代码:

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
    }
});
任何帮助都将不胜感激。媒体模式文档几乎不存在。

谢谢

2 个回复
SO网友:Rajilesh Panoli

您可以签出此链接https://codex.wordpress.org/Javascript_Reference/wp.media

jQuery(function($){

  // Set all variables to be used in scope
  var frame,
      metaBox = $(\'#meta-box-id.postbox\'), // Your meta box id here
      addImgLink = metaBox.find(\'.upload-custom-img\'),
      delImgLink = metaBox.find( \'.delete-custom-img\'),
      imgContainer = metaBox.find( \'.custom-img-container\'),
      imgIdInput = metaBox.find( \'.custom-img-id\' );

  // ADD IMAGE LINK



addImgLink.on( \'click\', function( event ){

    event.preventDefault();

    // If the media frame already exists, reopen it.
    if ( frame ) {
      frame.open();
      return;
    }

    // Create a new media frame
    frame = wp.media({
      title: \'Select or Upload Media Of Your Chosen Persuasion\',
      button: {
        text: \'Use this media\'
      },
      multiple: false  // Set to true to allow multiple files to be selected
    });


    // When an image is selected in the media frame...
    frame.on( \'select\', function() {

      // Get media attachment details from the frame state
      var attachment = frame.state().get(\'selection\').first().toJSON();

      // Send the attachment URL to our custom image input field.
      imgContainer.append( \'<img src="\'+attachment.url+\'" alt="" style="max-width:100%;"/>\' );

      // Send the attachment id to our hidden input
      imgIdInput.val( attachment.id );

      // Hide the add image link
      addImgLink.addClass( \'hidden\' );

      // Unhide the remove image link
      delImgLink.removeClass( \'hidden\' );
    });

    // Finally, open the modal on click
    frame.open();
  });


  // DELETE IMAGE LINK
  delImgLink.on( \'click\', function( event ){

    event.preventDefault();

    // Clear out the preview image
    imgContainer.html( \'\' );

    // Un-hide the add image link
    addImgLink.removeClass( \'hidden\' );

    // Hide the delete image link
    delImgLink.addClass( \'hidden\' );

    // Delete the image id from the hidden input
    imgIdInput.val( \'\' );

  });

});

SO网友:tru.d

试用:

wp.media.editor.get(wpActiveEditor).views._views[".media-frame-content"][0].views._views[""][1].collection.props.set({ignore:(+(new Date()))})
似乎有一个更简单的方法,但这对我来说是可行的!

更好的方法是:

wp.media.frame.content.get(\'gallery\').collection.props.set({‌​ignore: (+ new Date())});, 
在这种情况下,我正在刷新gallery选项卡。

尝试以上两种代码,看看哪一种最适合您。

结束

相关推荐

Media upload error

我是WP开发的新手。媒体上传时突然出现上传错误。甚至我也停用了所有插件。现在我使用的是WP的最新版本(4.1)。错误消息后,图像上载成功(uploads/2014/12/image\\u name)。但未在管理面板中列出,并显示错误消息。有什么建议吗?