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


12

我正在开发一个向媒体模式添加新标签的插件,并且我需要知道一种触发附件标签刷新的方式,以便它显示新添加的附件。这是我正在使用的代码:

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
    }
});

任何帮助,将不胜感激。媒体模式文档几乎不存在。

谢谢


IIRC只是骨干/下划线视图。换句话说,当您更新模型时,它应自行更新视图,因为“ ModelView”应触发该视图。
kaiser 2015年

好的,该this.controller.state().addAttachment()函数只是使用的AJAX调用wp.media.post(),因此我需要在此AJAX调用之后的某个地方触发一个假设的“模型更新”事件。有任何想法吗?
leemon

“有任何想法吗?” -目前没有。这是我必须花一些时间阅读核心内容的地方(我现在没有)。关于您的评论:提供MarkDown(请参阅“添加评论”按钮下方的“帮助”)。
kaiser 2015年

Answers:


2

您可以检出此链接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( '' );

  });

});

1

试用:

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())});, 

在这种情况下,我要刷新“图库”标签。

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


1
这对我有帮助!谢谢。
Siddhesh Shirodkar

1
是的,这也对我有用。
Amol Bhandari SJ
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.