Javascript 如何从Wordpress 3.5媒体上传程序获取选择

Javascript 如何从Wordpress 3.5媒体上传程序获取选择,javascript,media,wordpress-3.5,Javascript,Media,Wordpress 3.5,我正在尝试从新媒体上传器的默认实例中获取选择 我对默认显示的方式很满意,因此我不使用:- file_frame = wp.media.frames.file_frame = wp.media( { title: 'Select File', button: { text: jQuery( this ).data( 'uploader_button_text' ) }, multiple: false }); 只是 所以这显然不起作用 attach

我正在尝试从新媒体上传器的默认实例中获取选择

我对默认显示的方式很满意,因此我不使用:-

file_frame = wp.media.frames.file_frame = wp.media(
{
    title: 'Select File',
    button: {
        text: jQuery( this ).data( 'uploader_button_text' )
    },
    multiple: false
});
只是

所以这显然不起作用

attachment = file_frame.state().get('selection').first().toJSON();
但这也不是

wp.media.editor.state().get('selection').first().toJSON();
还是这个

wp.media.state().get('selection').first().toJSON();

那么我应该使用什么样的代码呢?

好的,我有一个很好的解决方案,直到有人希望能够发布如何正确地做到这一点

jQuery('.media-modal .type-image').on('click',function(index, element) {
var thisCollection = wp.media.model.Query.all._byCid;
setTimeout(function() { //we need this becuse the details area is not generated until after the click
    var thisEditUrl = jQuery('.details .edit-attachment').attr('href');
    var attachId = thisEditUrl.match(/post=([^&]+)/)[1];
    var attachmentAtts = '';
    jQuery.each(thisCollection, function(index,item) {
        if(this.id == attachId)
            attachmentAtts = this.attributes;
    });
},500);
});
由于设置超时,这并不理想,因为我们不知道填充.details区域需要多长时间,但经过一天半的尝试,我无法找到其他方法


希望有人知道如何正确处理;)

您可以尝试类似的方法(对于多选)。。。但不保证它能正常工作:

var selection = wp.media.editor.state().get('selection');

var attachment_ids = selection.map( function( attachment ) {
  attachment = attachment.toJSON();
  return attachment.id;
}).join();

此处答案的改编:

扩展
wp.media.view.Settings.AttachmentDisplay的
render
功能,该功能允许访问
this.controller.state().get('selection')


我知道这是一个旧的线程,但我偶然发现这从谷歌搜索获得所有附件ID时上传多个文件

我稍微采纳了Adal的答案,它非常有效:

                var selection = wp.media.state().get('selection');

                var attachment_ids = selection.map( function( attachment ) {
                    attachment = attachment.toJSON();
                    return attachment.id;
                }).join();

仅添加此作为参考,因为Adal当时对答案没有任何反馈(而且我还没有足够的声誉留下评论)。

在media-models.js的896中,有此返回我需要的内容,但我无法确定如何绑定侦听器以侦听“selection:single”:-this.\u single.trigger('selection:single',this.\u single,this);我一辈子都搞不清楚'this'指的是什么,towp.media.controller.state().get('selection')也不起作用,我现在已经尝试了成百上千种方法……我可以看到'selection:single'触发了这个.controller.state().get('selection'))但无法计算出这指的是什么…wp.media.view.Attachment.controller.state().get('选择')不起作用请,请,请有人帮我一下,我现在已经搜索了几乎一整天。我所需要的是所选项目的id…我已经尝试了我能想到的一切,现在几乎没有任何数据返回。请有人帮我一下…所以只是澄清一下,我需要将一个函数绑定到所选项目ion事件并从数据中获取id,或将函数绑定到单击媒体项,然后获取有关被单击项的数据。。。
function($) {
   var _AttachmentDisplay = wp.media.view.Settings.AttachmentDisplay;
   wp.media.view.Settings.AttachmentDisplay = _AttachmentDisplay.extend({
      render: function() {
         _AttachmentDisplay.prototype.render.apply(this, arguments);
         selection = this.controller.state().get('selection').first().toJSON();
     //now, for example:
         filename = selection.filename;
      }
   });
})();
                var selection = wp.media.state().get('selection');

                var attachment_ids = selection.map( function( attachment ) {
                    attachment = attachment.toJSON();
                    return attachment.id;
                }).join();