Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Angular route/state file from Begin to Code
app.config(['$stateProvider', '$urlRouterProvider', '$locationProvider',
function($stateProvider, $urlRouterProvider, $locationProvider) {
$stateProvider.state('root', {
url: '/',
templateUrl: 'root.html',
controller: 'RootCtrl'
})
.state('program', {
url: '/program/:program_id',
templateUrl: 'program.html',
resolve: {
program: function(ProgramCache, $stateParams, $q, $rootScope) {
log.debug("resolve...");
if ($stateParams.program_id === "new") {
return $q.when({}); // hack a resolved blank object
}
// otherwise, normally, return a promise that we will fetch program from cache or server
return ProgramCache.get($stateParams.program_id).then(function(programResponse) {
log.debug("StateProvider: program:resolve: got program with title " + programResponse.title);
$rootScope.meta.webAppTitle = programResponse.title;
return programResponse;
});
}
},
controller: function($scope, program) {
log.debug("program in program state controller is:");
log.debug(program);
// NOTE: i'm thinking this isn't necessary?
//$scope.program = program;
}
})
.state('program-list', {
url: '/programs',
controller: 'ListCtrl',
templateUrl: 'program_list.html',
data: {
menu: {
text: "All programs",
states: [
]
}
}
})
.state('program.run', {
url: '/run',
views: {
'programHeader': {
templateUrl: 'program_header.html'
},
'programMain': {
controller: 'RunCtrl',
templateUrl: 'program_run.html'
}
},
data: {
menu: {
text: "Run program",
states: [
"program-list",
"program.edit"
]
}
}
})
.state('program.edit', {
url: '/edit',
onEnter: function() {
log.debug("onEnter...");
},
data: {
menu: {
text: "Edit source code",
states: [
"program.run",
"program-list"
]
}
},
views: {
'programHeader': {
templateUrl: 'program_header.html'
},
'programMain': {
template: '<div program-editor program="program" kind="edit"></div>',
}
}
});
// For any unmatched url, redirect to / root
// this is necessary to turn "domain.com" or "domain.com/" into "domain.com/#/"
$urlRouterProvider.otherwise("/");
// was getting problems using the HTML5 History API
// $locationProvider.html5Mode(true);
}]);
app.run(['$state', '$rootScope', function($state, $rootScope) {
$rootScope.meta = {};
$rootScope.meta.webAppTitle = "Begin to code";
$rootScope.$on('$stateChangeStart', function(e, toState, toParams, fromState, fromParams) {
var href = $state.href(toState.name, {}, {absolute: true});
log.info("new state has url: " + href);
});
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment