dotJEM / angular-routing

Enhanced state based routing for Angular applications!
https://dotjem.github.io/angular-routing/
MIT License
75 stars 9 forks source link

Uncaught SyntaxError: Unexpected token [ when using array for state provider view key #126

Closed jh97uk closed 10 years ago

jh97uk commented 10 years ago

Im trying to make it so I have an array of predefined states which also define which view changes which. This is what I have so far:

var app = angular.module("kmanage", ['dotjem.routing']);
var pages = [
    {
        name: "login",
        path: "/login",
        page: "templates/login.html",
        view: "main"
    },
    {
        name: "register",
        path: "/register",
        page: "templates/register.html",    
        view: "main"
    },
    {
        name: "dashboard",
        path: "/dashboard",
        page: "templates/dash.html",    
        view: "main"
    }
];
app.config(function($locationProvider, $routeProvider, $stateProvider){
    $routeProvider.otherwise({redirectTo:"/home"});

    for(state in pages){
        $stateProvider.state(pages[state].name, {
            route:pages[state].path,
            views:{
                pages[state].view: {
                    template: pages[state].page
                }
            }
        });
    }

});

Just throws the error:Uncaught SyntaxError: Unexpected token [

Appologies if this is not a bug, just cant see why this wouldn't work.

jeme commented 10 years ago

Considering that this is a syntax error, I doubt very much this is something in the framework. It must be giving you a location of where this happens?

jh97uk commented 10 years ago

Oh god, sorry. Completely left out where the error occurs... Line 29 where its pages[state].view: {

jeme commented 10 years ago
app.config(function($locationProvider, $routeProvider, $stateProvider){
    $routeProvider.otherwise({redirectTo:"/home"});

    for(state in pages){
        $stateProvider.state(pages[state].name, {
            route:pages[state].path,
            views: { //This is invalid JavaScript... Just like it tells you, it expects pages to be a property, which is why it expects a : over a [... hence the syntax error.
                pages[state].view: { 
                    template: pages[state].page
                }
            }
        });
    }
});