2015-04-02 77 views
1

我做了一個控制器,從其他api獲取數據,並將其序列化爲.xls文件。sails.js視圖不加載

在我的網絡視圖中,我有一個按鈕「Create xls data」,它調用我的控制器。 然後,我的控制器使用xls下載鏈接和一個「下載數據」按鈕來對該視圖進行解析和重新加載。

我的看法import_export_data_page.ejs:
在開始按鈕「創建xls數據」顯示。

<% 

if(typeof link == 'undefined'){ 
%> 
<button type="button" class="btn btn-primary" id="button_get_xls_file">Create xls data</button> 
<% 
}else { 
%> 
<a href="<%= link %>"><button type="button" class="btn btn-primary"></button></a> 
<% 
} 
%> 

<script type="text/javascript"> 
$('#button_get_xls_file').click(function(){ 
var projectName = getCurrentServiceName(currentService); 
$.get("/api/export_xls_data", { url : currentService, projectname :  projectName}).done(function(data) { 
    }); 
}); 

</script> 

在我的控制器:

exportXLS : function(req,res) 
{ 
    var request = require('request'); 
    var categories; 
    //get informations 
    request(url + "/places", function (error, response, body) { 
      categories = JSON.parse(body); 
     /* 
     * Parsing tacks 3 seconds max 
     */ 
     //if file exists delete and create file 
     fs.exists(fileName, function (exists) { 
       if(exists){ 
        fs.unlinkSync(fileName); 
       } 
       fs.writeFile(fileName, xls, 'binary',function(err){ 
       if(err){ 
         console.log(err); 
         res.status(500); 
        return res.send(""); 
       }else{ 
        var downloadLink = req.headers.host+'/xls/'+currentProject.split(' ').join('')+'_data.xls'; 
        console.log(downloadLink); 
        return res.view('import_export_data_page',{link : downloadLink}); 
        }); 
       }); 

    } 

} 

我的文件被創建。我的鏈接可以工作,但我的視圖不會重新加載。

回答

2

您的視圖不會因爲您進行AJAX調用而重新加載。代碼中沒有任何東西可以告訴頁面重新加載視圖。

我想你想改變這一點:

$.get("/api/export_xls_data", { url : currentService, projectname :  projectName}).done(function(data) {}); 

到:

window.location.href = '/api/export_xls_data?url=' + currentService + '&projectname=' + projectName