2013-02-25 44 views
2

我試圖從新的媒體上傳的默認實例的選擇...如何從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 
}); 

只是

wp.media.editor.open(); 

所以這不明明工作

attachment = file_frame.state().get('selection').first().toJSON(); 

但同樣沒有這個

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

或本

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

那麼,什麼是代碼,我應該使用?

+0

media-models.js在896有這個返回我需要的東西,但我不能鍛鍊什麼來綁定偵聽器來偵聽'selection:single': - this._single.trigger('selection:single ',this._single,this);我不能爲我的生活制定出'這'是指什麼 – 2013-02-25 23:11:49

+0

wp.media.controller.state()。get('selection')不起作用要麼我現在已經嘗試了數百個東西...我可以看到'selection:single'觸發this.controller.state()。get('selection'),但無法解決這是什麼... – 2013-02-26 00:01:51

+0

wp.media.view.Attachment.controller.state() .get('selection')不起作用 – 2013-02-26 00:02:30

回答

0

好吧,我有一個哈克解決方案,這一點,直到有人在那裏希望可以張貼如何正確地做到這一點...

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

它並不理想,因爲,因爲我們不知道多久會採取的setTimeout的爲.details區域將被填充,但經過一天半的嘗試,我無法用任何其他方式。

希望有人在那裏知道如何做到這一點的權利;)

1

你可以嘗試這樣的事情(爲多選)......不能保證它的工作原理,但:

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

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

很酷,會嘗試一下並反饋... – 2013-04-22 11:04:32

1

https://wordpress.stackexchange.com/questions/87357/wordpress-media-manager-3-5-default-link-to

延長wp.media.view.Settings.AttachmentDisplayrender功能,然後可以訪問this.controller.state().get('selection')

從這裏回答的適應
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; 
     } 
    }); 
})(); 
0

我知道這是一個古老的線程,但我偶然發現了一個谷歌搜索在上傳多個文件時獲取所有附件id的情況。

我稍微適應阿達爾的答案,它完美的作品:

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

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

只需添加此作爲參考,因爲阿達爾上了答案沒有反饋回來,然後(我沒有足夠的聲譽留下評論)。

相關問題