Commit eef070ef21a687158d935868841b010ed2d1e2e5
1 parent
be5b00c2
Exists in
master
and in
1 other branch
Cleaning the House Task - first commit
Showing
6 changed files
with
49 additions
and
6 deletions
Show diff stats
src/app/components/noosfero-blocks/recent-documents/recent-documents.component.ts
... | ... | @@ -23,8 +23,10 @@ export class RecentDocumentsBlock { |
23 | 23 | this.profile = this.owner; |
24 | 24 | this.documents = []; |
25 | 25 | |
26 | - var limit = (this.block && this.block.settings) ? this.block.settings.limit : null || 5; | |
27 | - //FIXME get all text articles | |
26 | + let limit = (this.block && this.block.settings) ? this.block.settings.limit : null || 5; | |
27 | + // FIXME get all text articles | |
28 | + // FIXME make the getByProfile a generic method where we tell the type passing a class TinyMceArticle | |
29 | + // and the promise should be of type TinyMceArticle[], per example | |
28 | 30 | this.articleService.getByProfile(this.profile.id, { content_type: 'TinyMceArticle', per_page: limit }).then((response: any) => { |
29 | 31 | this.documents = response.data.articles; |
30 | 32 | this.documentsLoaded = true; | ... | ... |
src/lib/ng-noosfero-api/http/article.service.ts
... | ... | @@ -2,10 +2,12 @@ import { Injectable, Inject } from "ng-forward"; |
2 | 2 | import {Article} from "../../../app/models/interfaces"; |
3 | 3 | |
4 | 4 | @Injectable() |
5 | -@Inject("Restangular") | |
5 | +@Inject("Restangular", "$q") | |
6 | + | |
7 | + | |
6 | 8 | export class ArticleService { |
7 | 9 | |
8 | - constructor(private Restangular: any) { } | |
10 | + constructor(private Restangular: restangular.IService, private $q: ng.IQService, private $log: ng.ILogService) { } | |
9 | 11 | |
10 | 12 | create(profileId: number, article: Article) { |
11 | 13 | return this.Restangular.one('profiles', profileId).customPOST( |
... | ... | @@ -16,8 +18,23 @@ export class ArticleService { |
16 | 18 | ); |
17 | 19 | } |
18 | 20 | |
19 | - getByProfile(profileId: number, params?: any) { | |
20 | - return this.Restangular.one('profiles', profileId).customGET('articles', params); | |
21 | + // TODO create a handle ErrorFactory too and move handleSuccessFactory and handleErrorFactory | |
22 | + // to a base class (of course we will have to creates a base class too) | |
23 | + handleSuccessFactory<T>(deferred: ng.IDeferred<T>): (response: restangular.IResponse) => void { | |
24 | + let self = this; | |
25 | + let successFunction = (response: restangular.IResponse): void => { | |
26 | + this.$log.debug("Request successfull executed", this, response); | |
27 | + deferred.resolve(response.data); | |
28 | + }; | |
29 | + return successFunction; | |
30 | + } | |
31 | + | |
32 | + // TODO -> change all Restangular services to this approach "Return promise to a specific type" | |
33 | + // it makes easy consume the service | |
34 | + getByProfile<T>(profileId: number, params?: any): ng.IPromise<T> { | |
35 | + let deferred = this.$q.defer<T>(); | |
36 | + this.Restangular.one('profiles', profileId).customGET('articles', params).then(this.handleSuccessFactory(deferred)); | |
37 | + return deferred.promise; | |
21 | 38 | } |
22 | 39 | |
23 | 40 | getChildren(articleId: number, params?: any) { | ... | ... |