2
\$\begingroup\$

My Application looks something like this (Screenshot). To Control the URL Routing i am using a Angular Js Router. On Click of Store of the Side Panel the StoreList Appears. And on Click of the Edit, the portion template gets fetched from Spring MVC and displayed along with the details. I have written the Code for app.js provided in the below part.

Because data could not be shared between the controllers i have written a StoreService and then the listStore array and its getters and setters so that same can be accessed via the controllers. This works fine but :-

  1. can there be a better code to this or any Angular functionalities i can use which can really simplify the code ? OR

  2. if i am not violating the rules of this forumn, can i directly ask is that the right way to code ?

  3. Was there anyway to use one single Controller for a logical module (like Store CRUD) ?

enter image description here

app.js :-


    var storezillaadminapp = angular.module('storezilla-admin',['ngRoute']);

    storezillaadminapp.config(['$routeProvider',
            function($routeProvider) {
                $routeProvider.when("/liststores",{
                templateUrl:_contextPath+"/stores/getallstores",
                    controller : "StoreZillaAdminListController"
            });
            $routeProvider.when("/editstore/:id",{
                templateUrl:_contextPath+"/stores/geteditstore",
                controller : "StoreZillaAdminEditController"
            });
        }]);

    storezillaadminapp.service('StoreService',function($http){

       var listStores = [];

        this.getAllStores = function() { 
            console.log('Called....');
            return $http.get(_contextPath+'/stores');
        };

        this.getListStores = function() {
            return listStores;
        };

        this.setListStores = function(data) {
            listStores = data;
        };
    });

    storezillaadminapp.controller('StoreZillaAdminListController',function($scope,StoreService){
        StoreService.getAllStores().success(function(response){
            $scope.listStores = response;
            StoreService.setListStores(response);
        });
    });

    storezillaadminapp.controller('StoreZillaAdminEditController',function($scope,$routeParams,StoreService){
    $scope.store = StoreService.getListStores()[$routeParams.id];
});
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.