I need to trigger a CSV download, but window.open()
is failing in this context. No console error.
I'm calling exportRecords()
in Customer.vue
, which delegates the handling logic to services.js
via customer_actions.js
. At this point, I assume the scope of window
is the issue.
If I move window.open()
outside of this.exportCsv()
, it works -- although I have no data.
Customer.vue
<button @click.prevent="exportRecords">Export</button>
<script>
import * as actions from '../../vuex/customers_actions';
export default {
vuex: {
actions
},
data () {
return {
grid: {}
};
},
methods: {
exportRecords: function () {
this.exportCsv([this.$data.grid], function (response) {
window.open('data:text/csv;charset=utf-8,' + encodeURIComponent(response));
});
}
}
};
</script>
customers_actions.js
import { ApiService } from './services';
export const exportCsv = function (store, grids = [], successCallback) {
ApiService(
store,
{
noun: 'Customer',
verb: 'GetRecordsViewDataAsCSVFile',
data: {}
},
successCallback,
'csv'
);
};
services.js
export const ApiService = (store, options, successCallback, responseType = 'json') => {
let parameters = {};
store.dispatch('SET_BUSY');
Vue.http.post(API_URL, parameters, []).then((promise) => {
return promise.text();
}, (promise) => {
return promise.text();
}).then(response => {
store.dispatch('SET_NOT_BUSY');
successCallback(response);
});
};