在解析后,如何通过回调将内容从服务传递到另一个服务到控制器?

时间:2015-08-05 20:37:53

标签: javascript angularjs node.js csv callback

我有一个使用回调函数将内容传递给控制器​​的服务:

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

我正在努力想象这是如何工作的,这样我就可以向控制器传递第二个回调并仍然获得所需的内容。如何在解析后将内容从服务传递到服务器?

非常感谢你的时间。如果您需要任何其他信息或我不清楚,请告诉我。

此问题是this previous post

的扩展

1 个答案:

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

}]);