Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Article route / controller refactor #800

Closed

Conversation

trainerbill
Copy link
Contributor

This has been bothering me for awhile. I refactored the Article controller and routes to be more in line with ui-router.

  1. Separated controllers into specific CRUD actions
  2. Removed ng controller from views
  3. Added controllers to routes
  4. Prevent loading of article every time route loads if the article is the same id that is already in scope.article

Benefits

  1. More inline with ui-router standards
  2. Less API calls since we are checking to see if the right article is already added.
  3. Ability to check authentication in EditController

Obviously the tests are going to fail. I will fix them once I get buy in and the code is set.

@lirantal @ilanbiala @rhutchison @mleanos @codydaig

@trainerbill
Copy link
Contributor Author

Anyone have any comments?

function ($scope, $state, $stateParams, Articles) {
//Check if article is loaded if not load it
if ($scope.article === undefined || typeof $scope.article !== 'object' || $scope.article._id === undefined || $scope.article._id !== $stateParams.articleId) {
$scope.loadArticleById($stateParams.articleId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Article should come from route resolve, not from controller.

@rhutchison
Copy link
Contributor

I think it can be simplified with 2 controllers - ArticleListController, ArticleController. Controllers should be separate files.

@mleanos
Copy link
Member

mleanos commented Aug 18, 2015

+1 on using just two controllers, and as separate files.

@trainerbill There are a lot of changes revolving around the Articles module, right now. I have an open PR #807 that contradicts this one in a sense. I'm not even sure if my proposed changes are what we want to go with. But I wanted to make sure you were aware of it.

@trainerbill
Copy link
Contributor Author

Thanks @mleanos. I think that the 2 PR's can coexist. Just keep yours to only admin functionality. I really think the article module needs to be refactored separately.

@trainerbill
Copy link
Contributor Author

Alright gents. I moved the load and authcheck to the $state resolve. Split out into 2 controllers. The only thing I don't like is having a mock article resolve for the create route since you have to resolve article to use the ArticlesController.

controller: 'ArticlesController',
resolve: {
article: function (Articles, $stateParams, $state) {
return Articles.get({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just return Articles.get(..).$promise;

the 404 should be handled by the http interceptor (https://github.com/meanjs/mean/pull/796/files#diff-90593c1684669b4a0cc185ec424f0a24R26)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree... but I doubt they will merge this in without the interceptor and I don't know if they like the interceptors.... So we would need both PR's to merge together. Which I am alright with.

@trainerbill
Copy link
Contributor Author

Can I get a couple LGTM? I want to fix the tests but need to settle on the code first.

@codydaig
Copy link
Member

LGTM. Haven't tested, but like and agree with the concept.

@codydaig codydaig mentioned this pull request Aug 20, 2015
@rhutchison rhutchison mentioned this pull request Aug 20, 2015
15 tasks
return article;
});
}
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, how can the backend return something you're not authorised to see? You should get 403 in that case. Or am I missing something?

In any case, this is a lot of logic for route config. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we are not hitting the backend until the actual update of the article. So the article is loaded in scope when it is viewed. Now when you put /edit in the URL you will get to the edit route because we are not hitting the backend. If you tried to update you would get authorization error but that does not prevent you from getting to the route. This will.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ic, makes sense now. 👍

templateUrl: 'modules/articles/client/views/list-articles.client.view.html',
controller: 'ArticlesListController',
resolve:{
articles: function (Articles, $stateParams, $state) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't like having the articles list in the resolve, the service should still be a part of the controller.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? Do we not want the most recent data on the route resolve? Not sure why we would want to call it in the controller after moving everything else to resolve.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, resolve's on a route are better suited for when you're interacting with an instance of a resource/entity. When a view is dependent on specific data actually existing at the time of loading. In the case of lists, it's not critical that there's data when the view loads or even if such data exists.

The concern of the route config (resolve in this case) should be to make sure the view has everything it needs in order to function properly, and for authorization purposes.

@ilanbiala ilanbiala modified the milestones: 0.4.1, 0.4.x Aug 31, 2015
@rhutchison rhutchison mentioned this pull request Sep 1, 2015
3 tasks
@ilanbiala ilanbiala modified the milestones: 0.4.1, 0.4.2 Sep 1, 2015
},
resolve: {
article: function () {
return { };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no space, but on another note, why do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because article controller is now expecting to resolve an article. Its either send a blank object or split it to another controller

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems too hacky, I'll try to think of a better way.

@trainerbill
Copy link
Contributor Author

@ilanbiala This PR is really messed up. Evidently I merged master in a couple times. You care if I close and reopen with just the changes? This commit is killing me on rebase:

trainerbill@21fa7dc

Looks like I did something pretty dumb on it.

@ilanbiala
Copy link
Member

Please do whatever you need to keep it tidy. 👍

@trainerbill trainerbill mentioned this pull request Sep 2, 2015
@trainerbill
Copy link
Contributor Author

Closing in favor of #874

@trainerbill trainerbill closed this Sep 2, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants