javascript - 在 Angular 服务中重用/重构代码

标签 javascript json angularjs

我有一个 Angular 服务,它通过 JSON 请求获取数据并将生成的 JSON 对象发送到许多不同的 Controller 。

下面的代码是该服务的一个片段。 postData 函数是该服务中许多不同函数之一的示例(几乎所有函数都执行相同的操作,但只是发出不同的请求)。该服务中还有其他函数,名为 viewDatavisitorData 等...

// Service for making JSON requests
    myApp.factory('getData', ['$http', '$cookieStore', 
      function($http, $cookieStore) {
      return {
        postData: function() {

          // Store shared variables   
          var source = 
            ($cookieStore.get('tab') == '#/dashboard/2') ?
            source = $cookieStore.get('1234') :
            source = $cookieStore.get('5678');
          var month = $cookieStore.get('month'),
              year = $cookieStore.get('year');

          // Derive the number of days in the given month
          var month_days = (new Date(year, month, 0)).getDate();

          // Return the promise
          return $http({
            url: base_url + 'string', 
            method: "GET",

            // Set the proper parameters
            params: { 
              id: source,
              start: year + '-' + month + '-01',
              end: year + '-' + month + '-' + month_days,
              interval: 'day'
              }
          });
        },
        ...

我面临的问题是,在每个函数的开头,我必须包含这段代码,并且每个函数的代码完全相同。

// Store shared variables   
var source = 
  ($cookieStore.get('tab') == '#/dashboard/2') ?
  source = $cookieStore.get('1234') :
  source = $cookieStore.get('5678');
var month = $cookieStore.get('month'),
    year = $cookieStore.get('year');

// Derive the number of days in the given month
var month_days = (new Date(year, month, 0)).getDate();

但是,很难将其分解出来,因为如果我这样做,当我调用函数时, 值不会随更新的 $cookieStore 值一起更改。有没有一种官方方法可以让我在服务共享中拥有多个函数,也许是每次调用函数本身时都会调用的 super 函数?基本上 - Angular 处理这个问题的方法是什么?

最佳答案

我不确定这是否是您的目的,但您可以将逻辑封装在另一个服务中,并将其包含在您希望在依赖项中使用的服务中;

myApp.factory('getCookieDataSvc', [ '$cookieStore',function($cookieStore){
return {
    getCookieData: function(){
        var source =
            ($cookieStore.get('tab') == '#/dashboard/2') ?
                source = $cookieStore.get('1234') :
                source = $cookieStore.get('5678');
        var month = $cookieStore.get('month'),
            year = $cookieStore.get('year');

        // Derive the number of days in the given month
        var month_days = (new Date(year, month, 0)).getDate();

        return {
            source: source,
            month_days: month_days,
            month: month,
            year: year,
        }
    }
}
});

然后您可以在其他服务或 Controller 中调用它,如下所示:

myApp.factory('getData', ['$http', 'getCookieDataSvc', function($http, getCookieDataSvc) {
return {
    postData: function() {
        var cookieData = getCookieDataSvc.getCookieData();

        // Return the promise
        return $http({
            url: base_url + 'string',
            method: "GET",

            // Set the proper parameters
            params: {
                id: cookieData.source,
                start: cookieData.year + '-' + cookieData.month + '-01',
                end: cookieData.year + '-' + cookieData.month + '-' + cookieData.month_days,
                interval: 'day'
            }
        });
    }
}});

关于javascript - 在 Angular 服务中重用/重构代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24533763/

相关文章:

javascript - 单击按钮时自动填写表单

javascript - jquery等高脚本

Java JSON 解析

java - 使用 Jersey 在 POST 中检索 JsonObject

javascript - ui 选择 angularjs 设置输入值的最大长度(ui 选择匹配)

javascript - 如何在ejs标签中插入javascript变量

javascript - 将 JavaScript 文件转换为 dll 文件

python - 如何遍历并解析 json 文件的文件夹然后输出到单个文件

javascript - ngResource save() 方法回调的参数是什么

javascript - 为什么我无权访问在上层作用域中创建的变量