Restangular,选择并获取编辑路线上的子嵌套对象。

时间:2016-06-28 16:17:20

标签: javascript angularjs nested restangular

我的休息角有问题,我找不到解决方案,(也许我是菜鸟)。

所以我有这条路线(/scales/1/edit)

我正在使用此API:

   {
      "total": 2,
      "per_page": 10,
      "current_page": 1,
      "last_page": 1,
      "next_page_url": null,
      "prev_page_url": null,
      "from": 1,
      "to": 2,
      "data": [
        {
          "id": 1,
          "start_date": "2006-08-20 05:11:32",
          "end_date": "2009-10-18 23:03:06",
          "created_at": "2016-06-23 08:41:51",
          "updated_at": "2016-06-23 08:41:51",
          "unity": {
            "id": 224,
            "code": 20060810,
          },
          "user": [
            {
              "id": 1,
              "username": "Kiara Runolfsdottir",
              "created_at": "2016-06-23 15:56:56",
              "updated_at": "2016-06-23 15:56:56"
            },
            {
              "id": 2,
              "username": "Zoe Dach",
              "created_at": "2016-06-23 15:56:56",
              "updated_at": "2016-06-23 15:56:56"
            },
            {
              "id": 3,
              "username": "Mrs. Zelma Johnston III",
              "created_at": "2016-06-23 15:56:56",
              "updated_at": "2016-06-23 15:56:56"
            },
          ]
        }
      ]
    }

我需要显示附加到特定比例的用户(也有一个统一的比例)并更新用户信息。

所以我认为更新用户的路线会有所改变(但也许我错了)(/scales/1/user/2/edit/)

但此时我无法在(/scales/1/edit)上显示用户对象。

这是我的服务

'use strict';

angular.module('ng-laravel').service('ScaleService', function($rootScope, Restangular,CacheFactory) {
    /*
     * Build collection /scale
     */
    var _scaleService =  Restangular.all('scale');

    if (!CacheFactory.get('scalesCache')) {
        var scalesCache = CacheFactory('scalesCache');
    }

    /*
     * Get list of scales from cache.
     * if cache is empty, data fetched and cache create else retrieve from cache
     */
    this.cachedList = function() {
        // GET /api/scale
        if (!scalesCache.get('list')) {
            return this.list();
        } else{
            return scalesCache.get('list');
        }

    };


    /*
     * Get list of scales
     */
    this.list = function() {
        // GET /api/scale
        var data = _scaleService.getList();

        scalesCache.put('list',data);
        return data;
    };


    /*
     * Pagination change
     */
    this.pageChange = function(pageNumber,per_page) {
        // GET /api/scale?page=2
        return _scaleService.getList({page:pageNumber,per_page:per_page});
    };


    this.cachedShow = function(id) {
        // GET /api/scale/:id
        if (!scalesCache.get('show'+id)) {
            return this.show(id);
        } else{
            return scalesCache.get('show'+id);
        }
    };

    /*
     * Show specific scale by Id
     */
    this.show = function(id) {
        // GET /api/scale/:id
        var data = _scaleService.get(id);
        scalesCache.put('show'+id,data);
        return data;
    };


    /*
     * Create scale (POST)
     */
    this.create = function(scale) {
        // POST /api/scale/:id
        _scaleService.post(scale).then(function() {
            $rootScope.$broadcast('scale.create');
        },function(response) {
            $rootScope.$broadcast('scale.validationError',response.data.error);
        });
    };

    /*
     * Update scale (PUT)
     */
    this.update = function(scale) {
        // PUT /api/scale/:id
        scale.put().then(function() {
            $rootScope.$broadcast('scale.update');
        },function(response) {
            $rootScope.$broadcast('scale.validationError',response.data.error);
        });
    };


    /*
     * Delete scale
     * To delete multi record you should must use 'Restangular.several'
     */
    this.delete = function(selection) {
        // DELETE /api/scale/:id
        Restangular.several('scale',selection).remove().then(function() {
            $rootScope.$broadcast('scale.delete');
        },function(response){
            $rootScope.$broadcast('scale.not.delete');
        });
    };

    /*
     * Search in scale
     */
    this.search = function(query,per_page) {
        //console.log(query);
        // GET /api/permission/search?query=test&per_page=10
        if(query != ''){
            return _scaleService.customGETLIST("search",{query:query, per_page:per_page});
        }else{
            return _scaleService.getList();
        }
    }


}); 

我的模特:

"use strict";

var app = angular.module('ng-laravel', ['ui.select']).controller('ScaleEditCtrl', function($scope, ScaleService, UnityService, $stateParams, $http, resolvedItems, resolvedItems3,  $translatePartialLoader, Notification, trans) {

    /*
     * Edit mode scale
     */
    $scope.scale = resolvedItems;
    $scope.unitys = resolvedItems3;

    $scope.date = {
        startDate: resolvedItems.start_date,
        endDate: resolvedItems.end_date,
    };


    /*
     * Update scale
     */
    $scope.update = function(scale) {
        $scope.isDisabled = true;
        scale.start_date = $scope.date.startDate;
        scale.end_date = $scope.date.endDate;
        ScaleService.update(scale);
    };


    /********************************************************
     * Event Listeners
     * Scale event listener related to ScaleEditCtrl
     ********************************************************/
    // Edit scale event listener
    $scope.$on('scale.edit', function(scope, scale) {
        $scope.scale = scale;
    });

    // Update scale event listener
    $scope.$on('scale.update', function() {
        Notification({ message: 'scale.form.scaleUpdateSuccess', templateUrl: 'app/shared/views/ui-notification/success.tpl.html' }, 'success');
        $scope.isDisabled = false;
    });

    // scale form validation event listener
    $scope.$on('scale.validationError', function(event, errorData) {
        Notification({ message: errorData, templateUrl: 'app/shared/views/ui-notification/validation.tpl.html' }, 'warning');
        $scope.isDisabled = false;
    });


});

我能得到的最佳尝试是在编辑路线上拥有所有测试对象。使用getList()之类的:

 this.show = function(id) {

            // GET /api/scale/:id
            var data = _scaleService.getList('scaleuser');
            return data;
    }

但是有了这个我无法传递id,选择要编辑的正确数据对象。这个输出是$object : Array[2],我只需要选择的对象(id)。

1 个答案:

答案 0 :(得分:0)

好的,经过一天的战斗,最后我找到了一个解决我的编辑路线列表问题的方法: 使用javascript承诺简单的事情:

this.show = function(id) {
        // GET /api/scale/:id
        var data =  _scaleService.getList().then(function (users) {
            var data = users[id-1];
            scalesCache.put('show' + id, data);
            return data;
        }) 
      return data;
};