我有一个使用回调函数将内容传递给控制器的服务:
angular.module('piApp').service("dataRetrievalService", function () {
function getContents(callback) {
//Converter Class
var fs = require("fs");
var Converter = require("csvtojson").Converter;
var fileStream = fs.createReadStream("order.csv");
//new converter instance
var converter = new Converter({ constructResult: true });
//end_parsed will be emitted once parsing finished
converter.on("end_parsed", function (jsonObj) {
console.log(jsonObj); //here is your result json object
//getResult(jsonObj)
callback(jsonObj);
});
//read from file
fileStream.pipe(converter);
}
// public api
return {
getContents: getContents
}
})
此服务使用csvtojson node module将csv文件中的内容作为JSON获取。以下是使用它的控制器:
angular.module('piApp').controller('homeController', ['$scope', 'dataRetrievalService', function ($scope, dataRetrievalService) {
dataRetrievalService.getContents(function(contents) {
$scope.result = contents;
});
}]);
我想知道如何将解析逻辑正确地移动到注入dataRetrievalService
的另一个服务,并在解析后仍然将csv文件的内容提供给控制器。
所以我的新csvService
将是
angular.module('piApp').service("csvService", function () {
// public methods
function getCsvAsJSON(callback) {
//Converter Class
var fs = require("fs");
var Converter = require("csvtojson").Converter;
var fileStream = fs.createReadStream("Contents/Product Groups/orderTest2.csv");
//new converter instance
var converter = new Converter({ constructResult: true });
//end_parsed will be emitted once parsing finished
converter.on("end_parsed", function (jsonObj) {
console.log(jsonObj); //here is your result json object
callback(jsonObj);
});
//read from file
fileStream.pipe(converter);
}
// public api
return {
getCsvAsJSON: getCsvAsJSON
}
})
我的dataRetrievalService
变得像
angular.module('piApp').service("dataRetrievalService", ['csvService', function (csvService) {
function getContents() {
csvService.getContents(function (contents) {
this.result = contents; //should I be using another callback here instead?
});
}
// public api
return {
getContents: getContents
}
}])
我正在努力想象这是如何工作的,这样我就可以向控制器传递第二个回调并仍然获得所需的内容。如何在解析后将内容从服务传递到服务器?
非常感谢你的时间。如果您需要任何其他信息或我不清楚,请告诉我。
的扩展答案 0 :(得分:2)
分离加载和解析例程是个好主意。但是,使用promises更方便,而不是回调。这就是服务在这种情况下的样子:
angular.module('piApp').service("csvService", function($q) {
// public methods
function getCsvAsJSON() {
var deferred = $q.defer();
var fs = require("fs");
var Converter = require("csvtojson").Converter;
var fileStream = fs.createReadStream("Contents/Product Groups/orderTest2.csv");
var converter = new Converter({constructResult: true});
converter.on("end_parsed", function(jsonObj) {
deferred.resolve(jsonObj);
});
fileStream.pipe(converter);
return deferred.promise;
}
return {
getCsvAsJSON: getCsvAsJSON
}
});
angular.module('piApp').service("dataRetrievalService", ['csvService', function(csvService) {
function getContents() {
return csvService.getCsvAsJSON();
}
return {
getContents: getContents
}
}]);
angular.module('piApp').controller('homeController', ['$scope', 'dataRetrievalService', function ($scope, dataRetrievalService) {
dataRetrievalService.getContents().then(function(contents) {
$scope.result = contents;
});
}]);