Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Code is executed second time with the wrong parameter
Message
 
 
À
Tous
Information générale
Forum:
Javascript
Catégorie:
Autre
Titre:
Code is executed second time with the wrong parameter
Divers
Thread ID:
01622436
Message ID:
01622436
Vues:
107
Hi everybody,

I am getting a weird behavior and trying to figure this out. May be putting this problem into words will help finding a solution.

My MVC controller has the following code:
  [Route("")]
        public ActionResult Index()
        {            
            ViewBag.departmentId = 0;
            ViewBag.categoryId = 0;
            ViewBag.itemId = 0;
            ViewBag.subCategoryId = 0;
            return View();
        }

        [Route("Department/{departmentId}")]
        public ActionResult DepartmentById(int departmentId)
        {
            ViewBag.departmentId = departmentId;
            ViewBag.categoryId = 0;
            ViewBag.itemId = 0;
            ViewBag.subCategoryId = 0;
            return View("Index");
        }

        [Route("Category/{categoryId}")]
        public ActionResult CategoryById(int categoryId)
        {
            ViewBag.categoryId = categoryId;
            ViewBag.departmentId = 0;
            ViewBag.itemId = 0;
            ViewBag.subCategoryId = 0;
            return View("Index");
        }

        [Route("Item/{itemId}")]
        public ActionResult ItemById(int itemId)
        {
            ViewBag.categoryId = 0;
            ViewBag.departmentId = 0;
            ViewBag.itemId = itemId;
            ViewBag.subCategoryId = 0;
            return View("Index");
        }

        [Route("SubCategory/{subCategoryId}")]
        public ActionResult SubCategoryById(int subCategoryId)
        {
            ViewBag.categoryId = 0;
            ViewBag.departmentId = 0;
            ViewBag.itemId = 0;
            ViewBag.subCategoryId = subCategoryId;
            return View("Index");
        }
and I invoke the page using max4Sales/SubCategory/2 syntax so the last route fires.

Now, the index view has the following:
<div ng-controller="max4salesController">   

    <div sm-search-slider="$parent.showSearch" class="pull-left col-md-8 sm-search-list" 
    ng-controller="max4salesSearchController" ng-init="setParameters(@ViewBag.departmentId, @ViewBag.categoryId, @ViewBag.itemId, @ViewBag.subCategoryId)">
and at the bottom it has
<div class="edit-container ng-cloak" ng-show="!isEditLoading" ui-view autoscroll="false">

    </div>
In my js controller I have the following at the top:
 $scope.searchParameters = { departmentId: null, categoryId: null, itemId: null, subCategoryId: null };
            $scope.showSearchControls = true;

            $scope.setParameters = function (departmentId, categoryId, itemId, subCategoryId) {
                if (departmentId == 0 && categoryId == 0 && itemId == 0 && subCategoryId == 0)
                    $scope.showSearchControls = true;
                else
                    $scope.showSearchControls = false;

                $scope.searchParameters.departmentId = departmentId;
                $scope.searchParameters.categoryId = categoryId;
                $scope.searchParameters.itemId = itemId;
                $scope.searchParameters.subCategoryId = subCategoryId;

                setSearchTable();
                if ($scope.showSearchControls)
                    loadSearchCriteria();
                //     window.console && console.log('MaxSaleSearchController setParameters is firing...')

                doSearch();
            }
and also I have the following states defined at the top:
app.config(['$stateProvider', function ($stateProvider) {
        $stateProvider.state('home', {
            url: '/',
            controller: 'max4salesController',
            template: ''
        }).state('edit', {
            url: '/edit/:id',
            controller: 'max4salesCrudController',
            templateUrl: '/CapacityControl/max4sales/editForm'
        }).state('new', {
            url: '/new',
            controller: 'max4salesCrudController',
            templateUrl: '/CapacityControl/max4sales/editForm'
        }).state('department', {
            url: '/new/:departmentId',
            controller: 'max4salesCrudController',
            templateUrl: '/CapacityControl/max4sales/editForm'
        }).state('category', {
            url: '/new/:categoryId',
            controller: 'max4salesCrudController',
            templateUrl: '/CapacityControl/max4sales/editForm'
        }).state('item', {
            url: '/new/:itemId',
            controller: 'max4salesCrudController',
            templateUrl: '/CapacityControl/max4sales/editForm'
        }).state('subCategory', {
            url: '/new/:subCategoryId',
            controller: 'max4salesCrudController',
            templateUrl: '/CapacityControl/max4sales/editForm'
        });
    }]);
Also, in the doSearch procedure I have the following after retrieving data:
 if (data.totalCount < 1) {
                       
                            $scope.alertType = 'info';
                            $scope.alertMessage = resourceFactory.getResource('Messages', 'noSearchResults');
                            if (!$scope.showSearchControls) {
                                    $scope.new(); // call the new function right away
                            }
                    }
                    //if (data.totalCount == 1 && !keepForm) {
                    //    $scope.loadView($scope.results[0]);
                    //}
                    $scope.table.pageCount = data.pageCount;
                    $scope.table.pageSize = data.pageSize;
                    $scope.table.pageNumber = data.pageNumber;
                    $scope.table.totalCount = data.totalCount;
                    $rootScope.$broadcast('sm:resizeContainer');
                    $scope.isSearchLoading = false;
                    $timeout.cancel($scope.loadingTimeout);
The idea is that if I invoked the form with one of the parameters (e.g. SubCategory/2) and the search returned 0 rows then I want to also show the edit form right away to enter new row.

So, my new function now is the following:
 $scope.new = function () {

                $scope.selections.selectedId = 0;
                $scope.selections.typeDescription = '';
                if ($scope.searchParameters.departmentId != 0) {
                    $state.go('department', { departmentId: $scope.searchParameters.departmentId });
                }
                if ($scope.searchParameters.categoryId != 0) {
                    $state.go('category', { categoryId: $scope.searchParameters.categoryId });
                }
                if ($scope.searchParameters.itemId != 0) {
                    $state.go('item', { itemId: $scope.searchParameters.itemId });
                }

                if ($scope.searchParameters.subCategoryId != 0) {
                    $state.go('subCategory', { subCategoryId: $scope.searchParameters.subCategoryId });
                }

                if ($scope.showSearchControls)
                    $state.go('new', {}, { reload: true });

                //       $scope.$parent.showSearch = false; // hide the search form
            };
And in the controller associated with all these states I have now the following:
var init = function () {
                $scope.showForm = false;
                $scope.disableAction = false;
                $scope.isEditLoading = false;
                load($stateParams.id);
            };

            var load = function (id) {
                if (id) {
                    loadMax4Sale(id);
                } else {
                    {
                        var depId, catId, itmId, sbcId;
                        if ($stateParams.departmentId)
                        {
                            depId = $stateParams.departmentId;
                        }
                        else
                        {
                            depId = 0;
                        }

                        if ($stateParams.categoryId) {
                            catId = $stateParams.categoryId;
                        }
                        else {
                            catId = 0;
                        }
                        if ($stateParams.itemId) {
                            itmId = $stateParams.itemId;
                        }
                        else {
                            itmId = 0;
                        }
                        if ($stateParams.subCategoryId) {
                            sbcId = $stateParams.subCategoryId;
                        }
                        else {
                            sbcId = 0;
                        }

                        loadNewMax4Sale(depId, catId, itmId, sbcId);
                    }
                }
            };
Here is what is happening. When I debug I can see that the method associated with the loadNewMax4Sale is called twice. The first time correctly with the subCategoryId = 2 and then immediately with the departmentId = 2 (which is incorrect). I don't understand from where this second call is coming from.

So, I am looking for ideas of how to debug this problem.

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform