[英]Rails Asset Pipeline Paths In Runtime Javascript
我有一個JavaScript文件,該文件使用了從Rails模型生成並由控制器放入頁面的JSON文件。
在此JSON文件中,有許多節點,每個節點都有一個ID,我需要將其轉換為圖像路徑。 在開發中,我知道圖像的位置,因此可以遍歷do:
var imagePath = 'some/path/'+node.id+'.png';
但是,我正在使用指紋識別,因此在登台和生產中,我需要一種方法來將此路徑解析為指紋路徑。
據我所知,我需要公開路徑映射-指紋路徑,這樣我才能有效地執行以下操作:
var fingerprintedImagePath = fingerprintedPathsMap[imagePath];
為此,我需要訪問清單或任何Rails從控制器內或視圖內(在.erb標記內)存儲清單的東西。
如何訪問指紋文件列表? 還是有更好的方法讓JavasScript訪問僅在客戶端已知的資產路徑。
對這種方法並不完全滿意,但是我有以下幾點:
注意: Gon gem可將對象插入dom,以便JavaScript可以訪問它們。
在我的控制器中:
gon.asset_host = Rails.configuration.action_controller.asset_host || ''
gon.manifest = Rails.configuration.assets.digests
在我的JavaScript中:
var manifest = gon.manifest;
var fileName = panelName+'_'+layerName+'.png';
var image_path = 'resources/comics/'+comicName+'/'+fileName;
var hosted_path = image_path;
if(gon.asset_host) {
var fingerprinted_path = manifest[image_path];
hosted_path = gon.asset_host+'/assets/'+fingerprinted_path;
}else{
hosted_path = '/assets/'+hosted_path;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.