damien marchand
damien marchand

Reputation: 864

sails.js view does not load

I made a controller which get data from a rest api and serializes them to .xls file.

In my web view I have a button with "Create xls data" which call my controller. Then my controller sierialises and reload this view with the xls download link and a "Download data" button.

My view import_export_data_page.ejs:
At start button "Create xls data" is displayed.

<%

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>

In my controller :

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

    }

}

My file is created. My link works but my view is not reloaded.

Upvotes: 1

Views: 482

Answers (1)

Meeker
Meeker

Reputation: 5979

Your view is not reloaded because your making an AJAX call. There is nothing in your code to tell the page to reload the view.

I think you want to change this:

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

to:

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

Upvotes: 2

Related Questions