angular-ui / AngularJS-StyleGuide

Example of scalable architecture for my NG-Conf 2014 talk
http://www.youtube.com/watch?v=Iw-3qgG_ipU
284 stars 53 forks source link

When would you getTasks() #12

Closed marcfallows closed 9 years ago

marcfallows commented 10 years ago

Given you hit the individual project state, which resolves a project to be injected into to the ProjectController.

Say you want to display the tasks for the project immediately for that state. At what point would you call the getTasks()? Would you do this right away in the ProjectController?

module.controller 'Project', ($scope, project) ->
    $scope.project = project
    $scope.project.getTasks()

Or, would you make it part of the resolve?

resolve:
    project: (AppObject, $stateParams) ->
        AppObject.getProject($stateParams.projectId).then (project) =>
            project.getTasks().then() =>
                project

Or maybe you were thinking something completely different?

The resolve works quite well with states, but once there aren't states I'm a little unsure on how to fetch the children.

Thanks for any help.

ProLoser commented 10 years ago

One of the awesome parts about ui-router's resolve chaining is you can make them siblings:

resolve:
  project: (AppObject, $stateParams) ->
    AppObject.getProject($stateParams.projectId)
  tasks: (project) ->
    project.getTasks()

Of course this will chain the queries and perhaps slow down the loading of the page. It just occurred to me that if ui-router doesn't load children states async then you can get a performance boost by placing the project's tasks into a substate, but this may not necessarily make sense for your navigation which is why the above solution should work too.

marcfallows commented 10 years ago

Amazingly quick response. I didn't know ui-router would chain those resolves, very cool.

Substates is more what I had in mind, but I'm not sure how you would load the tasks into a substate of the project by default. The ui-router setup in that case isn't very clear to me so I'll need to dig into the docs a bit more.

ProLoser commented 10 years ago

Yeah that SPECIFIC scenario has been sorta tricky for me in the past. Like lets say you get sent to project but really the default is always project.tasks. What I've had to resort to doing is essentially putting a check to see if you were going to a specific substate already and if not, redirecting you to project.tasks in the controller of project. It does cause the project state to finish loading and give the user more UI sooner, but it's been a bit annoying to code (you have to put a watcher on the routechange event and if someone goes from project.whatever to project you push them back into tasks) but perhaps this is something worth requesting on ui-router.

It's similar to the concept of an abstract state, like project essentially IS an abstract state, but if you DO navigate to it, you get pushed into a substate. In fact... https://github.com/angular-ui/ui-router/issues/1235