Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular UI-Router dynamic routing based on slug from API Ajax Call. Load view based on slug

Examples slugs in server database accessible through API:

{slug: "john-smith",type: "user"}
{slug: "microsoft-technologies",type: "company"}

scenario 1 : user view & controller : http://localhost/john-smith

.state('user', {
    url: '/:user',
    templateUrl: 'partial-user.html',
    controller: 'userCtrl'
})

scenario 2 : company view & controller : http://localhost/microsoft-technologies

.state('company', {
    url: '/:company',
    templateUrl: 'partial-company.html',
    controller: 'companyCtrl'
})

Now I want to make make a dynamic state based the slug getting from API Call to the server.

I written a imaginary code. But I'm not getting way to achieve

// Example URL http://localhost/john-smith
.state('hybrid', {
    // /john-smith
    url: '/:slug',
    templateUrl: function () {
        return "partial-"+type+".html"
    },
    controllerProvider: function (rt) {
        return type+'Controller'
    },
    resolove: {
        type: function ($http, $stateParams) {
            $http.get({
                method: "GET",
                url: "http://localhost/api/" + $stateParams.slug
            }).success(function(response, status, headers, config){
                //response = {slug: "john-smith",type: "user"}
                return response.type
            })
            return 
        }
    }    
})
like image 424
Manjesh V Avatar asked May 23 '15 06:05

Manjesh V


1 Answers

There is a working plunker.

It comes from similar issue: AngularJS ui-router - two identical route groups

In case, I do understand your aim properly, this would be the adjusted state definition (I just skipped the $http and server response part, just working with passed parameter):

.state('hybrid', {
    // /john-smith
    url: '/{slug:(?:john|user|company)}',
    templateProvider: ['type', '$templateRequest',
      function(type, templateRequest) 
      {
        var tplName = "tpl.partial-" + type + ".html";
        return templateRequest(tplName);
      }
    ],
    controllerProvider: ['type',
      function(type) 
      {
        return type + 'Controller';
      }
    ],
    resolve: {
      type: ['$http', '$stateParams',
        function($http, $stateParams) {
          /*$http.get({
            method: "GET",
            url: "http://localhost/api/" + $stateParams.slug
        }).success(function(response, status, headers, config){
            //response = {slug: "john-smith",type: "user"}
            return response.type
        })*/
          return $stateParams.slug
        }
      ]
    }
})

One change is the resolove : {} became: resolve : {}. Another is fixture of the controller def (rt vs type). And also, we do profit from built in features templateProvider and $templateRequest (similar here: Angular ui.router reload parent templateProvider)

check that in action here

EXTEND, including the $http part (extended plunker)

Let's adjust (for plunker purposes) the server part to return this info as data.json:

{
 "john-smith": {"type": "user"},
 "lady-ann": {"type": "user"},
 "microsoft-technologies" : {"type": "company" },
 "big-company" : {"type": "company" },
 "default": {"type" : "other" }
}

And these links:

<a href="#/john-smith">
<a href="#/lady-ann">

<a href="#/microsoft-technologies">
<a href="#/big-company">

<a href="#/other-unknown">

Will be easily managed by this adjusted state def:

  .state('hybrid', {
    // /john-smith
    url: '/:slug',
    templateProvider: ['type', '$templateRequest',
      function(type, templateRequest) 
      {
        var tplName = "tpl.partial-" + type + ".html";
        return templateRequest(tplName);
      }
    ],
    controllerProvider: ['type',
      function(type) 
      {
        return type + 'Controller';
      }
    ],
    resolve: {
      type: ['$http', '$stateParams',
        function($http, $stateParams) {
          return $http.get("data.json")
            .then(function(response){
              var theType = response.data[$stateParams.slug]
                  ||response.data["default"]
              return theType.type
            })
        }
      ]
    }
  })

Check that updated stuff here

like image 137
Radim Köhler Avatar answered Oct 20 '22 00:10

Radim Köhler