Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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];
});
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.