-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(articles): ArticlesService extended $resource #1266
feat(articles): ArticlesService extended $resource #1266
Conversation
If we're happy with these changes, and going this direction with extending the Angular Resource objects, I'll refactor the rest of the client-side Articles code to follow this pattern. For instance, I'll add a method for the I'll also add some extra logic to the |
@mleanos take a look at angular/angular.js#10692 (comment), An Angular.js member suggests a slightly different style, maybe see if there's any reason to do it one way over the other? |
I originally came across this implementation, when investigating #1149. However, when I came across http://sauceio.com/index.php/2014/07/adding-custom-methods-to-data-models-with-angular-resource/, i like it much better. Using var Article = $resource('api/articles/:articleId', {
articleId: '@_id'
}, {
update: {
method: 'PUT'
}
});
// instance methods
angular.extend(Article.prototype, {
createOrUpdate: function () {
var article = this;
return createOrUpdate(article);
}
});
// Class methods
angular.extend(Article, {
find: function () {
var articlesResource = this;
return articlesResource.query();
}
});
return Article;
function createOrUpdate(article) {
if (article._id) {
return article.$update();
} else {
return article.$save();
}
}
} Using |
@codydaig @ilanbiala @lirantal Any thoughts on this implementation? |
@ilanbiala Thoughts? |
I personally am a huge fan of angular-model-factory. |
|
||
return Article; | ||
|
||
function createOrUpdate(article) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just consider moving this above the return, some people might get confused and forget about hoisting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having the function declarations after the return statement make it easier to read, and understand the implementation.
It follows this style from John Papa's guide. I don't feel super strong about it, but I think it would help others that are following, or aware of, the style-guide.
LGTM other than the one code order concern I mentioned above. |
@rhutchison I really like the idea of having client-side model's. However, angular-model-factory didn't really impress me. I felt like it added more complexity than it's worth to the project. I'd love to see how you would integrate it into this framework though. Perhaps, there's a benefit that I just didn't see. Our framework is quite lightweight, so it might be better to give our user's a simple approach, and let them decide if they wanted to implement something like angular-model-factory. |
Thanks! I think it is useful as example of how to use the service to create a separate client side data layer instead of preparing your data in the controller. |
@mleanos other than adding tests, which may or may not be necessary, LGTM. |
I'm pretty sure we already have tests for this functionality. I'll double check though. |
@mleanos if we don't need to add tests, LGTM. |
Extends the ArticlesService $resource object to include a custom method for creating, or updating, an Article instance. Related meanjs#1260
d6c7bf9
to
383a3a6
Compare
@ilanbiala I rebased this and added some additional error/callback handling from inside the service. I verified that we already have test coverage for this functionality. We're testing the @codydaig @ilanbiala @lirantal @simison LGTY? If so, I'll merge. |
LGTM. |
@meanjs/contributors I'll merge this in in the next few days, if there are no objections & I can get another LGTM. I just want to make sure we're still ok with this change. |
I guess it's ok by me. |
LGTM. |
Extends the ArticlesService $resource object to include a custom method for creating, or updating, an Article instance.
Related #1260