Commit 8ba08a192e33d60e891c855e04c989427a2bf91c
1 parent
4f1e59c1
Exists in
master
and in
18 other branches
added eventemitters to restangular_service base class. add getBlock method to bl…
…ock.service. change on statistics block to make it refresh after add or remove an article
Showing
6 changed files
with
85 additions
and
37 deletions
Show diff stats
src/app/article/article-default-view.component.ts
| ... | ... | @@ -5,7 +5,7 @@ import {MacroDirective} from "./macro/macro.directive"; |
| 5 | 5 | import {ArticleToolbarHotspotComponent} from "../hotspot/article-toolbar-hotspot.component"; |
| 6 | 6 | import {ArticleContentHotspotComponent} from "../hotspot/article-content-hotspot.component"; |
| 7 | 7 | import {ArticleService} from "./../../lib/ng-noosfero-api/http/article.service"; |
| 8 | - | |
| 8 | +import {NotificationService} from "./../shared/services/notification.service"; | |
| 9 | 9 | /** |
| 10 | 10 | * @ngdoc controller |
| 11 | 11 | * @name ArticleDefaultView |
| ... | ... | @@ -17,15 +17,15 @@ import {ArticleService} from "./../../lib/ng-noosfero-api/http/article.service"; |
| 17 | 17 | selector: 'noosfero-default-article', |
| 18 | 18 | templateUrl: 'app/article/article.html' |
| 19 | 19 | }) |
| 20 | -@Inject("$state", ArticleService) | |
| 20 | +@Inject("$state", ArticleService, NotificationService) | |
| 21 | 21 | export class ArticleDefaultViewComponent { |
| 22 | 22 | |
| 23 | 23 | @Input() article: noosfero.Article; |
| 24 | 24 | @Input() profile: noosfero.Profile; |
| 25 | 25 | |
| 26 | - constructor(private $state: ng.ui.IStateService, public articleService: ArticleService) { | |
| 26 | + constructor(private $state: ng.ui.IStateService, public articleService: ArticleService, protected notificationService: NotificationService) { | |
| 27 | 27 | // Subscribe to the Article Removed Event |
| 28 | - this.articleService.subscribeToArticleRemoved((article: noosfero.Article) => { | |
| 28 | + this.articleService.subscribeToModelRemoved((article: noosfero.Article) => { | |
| 29 | 29 | if (this.article.parent) { |
| 30 | 30 | this.$state.transitionTo('main.profile.page', { page: this.article.parent.path, profile: this.article.profile.identifier }); |
| 31 | 31 | } else { |
| ... | ... | @@ -35,8 +35,9 @@ export class ArticleDefaultViewComponent { |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | delete() { |
| 38 | - this.articleService.removeArticle(this.article).catch((cause: any) => { | |
| 39 | - throw new Error(`Problem removing the article: ${cause}`); | |
| 38 | + this.articleService.remove(this.article).catch((cause: any) => { | |
| 39 | + // TODO - Montar mensagem de erro com a causa | |
| 40 | + this.notificationService.error({ message: "article.default_view.remove.failed"}); | |
| 40 | 41 | }); |
| 41 | 42 | } |
| 42 | 43 | ... | ... |
src/app/layout/blocks/recent-documents/recent-documents-block.component.ts
| 1 | 1 | import {Component, Inject, Input} from "ng-forward"; |
| 2 | 2 | import {BlockService} from "../../../../lib/ng-noosfero-api/http/block.service"; |
| 3 | -import {ArticleService} from "./../../../../lib/ng-noosfero-api/http/article.service" | |
| 4 | -import {Arrays} from "./../../../../lib/util/arrays" | |
| 3 | +import {ArticleService} from "./../../../../lib/ng-noosfero-api/http/article.service"; | |
| 4 | +import {Arrays} from "./../../../../lib/util/arrays"; | |
| 5 | 5 | |
| 6 | 6 | @Component({ |
| 7 | 7 | selector: "noosfero-recent-documents-block", |
| ... | ... | @@ -28,12 +28,12 @@ export class RecentDocumentsBlockComponent { |
| 28 | 28 | }); |
| 29 | 29 | this.watchArticles(); |
| 30 | 30 | } |
| 31 | - | |
| 31 | + | |
| 32 | 32 | watchArticles() { |
| 33 | 33 | this.articleService.subscribeToArticleRemoved((article: noosfero.Article) => { |
| 34 | 34 | Arrays.remove(this.documents, article); |
| 35 | 35 | }); |
| 36 | - } | |
| 36 | + } | |
| 37 | 37 | |
| 38 | 38 | openDocument(article: any) { |
| 39 | 39 | this.$state.go("main.profile.page", { page: article.path, profile: article.profile.identifier }); | ... | ... |
src/app/layout/blocks/statistics/statistics-block.component.ts
| 1 | -import {Input, Inject, Component} from "ng-forward"; | |
| 2 | - | |
| 1 | +import {Input, Inject, Component, provide} from "ng-forward"; | |
| 2 | +import {ArticleService} from "./../../../../lib/ng-noosfero-api/http/article.service"; | |
| 3 | +import {BlockService} from "./../../../../lib/ng-noosfero-api/http/block.service"; | |
| 3 | 4 | @Component({ |
| 4 | 5 | selector: "noosfero-statistics-block", |
| 5 | 6 | templateUrl: 'app/layout/blocks/statistics/statistics-block.html' |
| 6 | 7 | }) |
| 7 | 8 | |
| 9 | +@Inject(ArticleService, BlockService) | |
| 8 | 10 | export class StatisticsBlockComponent { |
| 9 | 11 | @Input() block: noosfero.StatisticsBlock; |
| 10 | 12 | @Input() owner: any; |
| 13 | + | |
| 14 | + constructor(articleService: ArticleService, blockService: BlockService) { | |
| 15 | + // watches for article being removed | |
| 16 | + // to update comments and tag statistics, which would | |
| 17 | + // changed after removing an article | |
| 18 | + articleService.subscribeToModelRemoved(() => { | |
| 19 | + blockService.getBlock<noosfero.StatisticsBlock>(this.block.id) | |
| 20 | + .then(blockFromAPI => this.block = blockFromAPI); | |
| 21 | + }); | |
| 22 | + | |
| 23 | + articleService.subscribeToModelAdded(() => { | |
| 24 | + blockService.getBlock<noosfero.StatisticsBlock>(this.block.id) | |
| 25 | + .then(blockFromAPI => this.block = blockFromAPI); | |
| 26 | + }); | |
| 27 | + } | |
| 11 | 28 | } | ... | ... |
src/lib/ng-noosfero-api/http/article.service.ts
| ... | ... | @@ -7,8 +7,6 @@ import {NoosferoRootScope} from "./../../../app/shared/models/interfaces"; |
| 7 | 7 | @Inject("Restangular", "$q", "$log", ProfileService) |
| 8 | 8 | export class ArticleService extends RestangularService<noosfero.Article> { |
| 9 | 9 | |
| 10 | - private articleRemoved: EventEmitter<noosfero.Article> = new EventEmitter<noosfero.Article>(); | |
| 11 | - | |
| 12 | 10 | constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService, protected profileService: ProfileService) { |
| 13 | 11 | super(Restangular, $q, $log); |
| 14 | 12 | } |
| ... | ... | @@ -24,28 +22,22 @@ export class ArticleService extends RestangularService<noosfero.Article> { |
| 24 | 22 | }; |
| 25 | 23 | } |
| 26 | 24 | |
| 27 | - removeArticle(article: noosfero.Article) { | |
| 28 | - let restRequest: ng.IPromise<noosfero.RestResult<noosfero.Article>> = this.remove(article); | |
| 29 | - let deferred = this.$q.defer<noosfero.RestResult<noosfero.Article>>(); | |
| 30 | - restRequest.then((result: any) => { | |
| 31 | - this.notifyArticleRemovedListeners(article); | |
| 32 | - }).catch(this.getHandleErrorFunction(deferred)); | |
| 33 | - return deferred.promise; | |
| 34 | - } | |
| 25 | + // removeArticle(article: noosfero.Article) { | |
| 26 | + // // let restRequest: ng.IPromise<noosfero.RestResult<noosfero.Article>> = this.remove(article); | |
| 27 | + // // let deferred = this.$q.defer<noosfero.RestResult<noosfero.Article>>(); | |
| 28 | + // // restRequest.then((result: any) => { | |
| 29 | + // // this.notifyArticleRemovedListeners(article); | |
| 30 | + // // }).catch(this.getHandleErrorFunction(deferred)); | |
| 31 | + // // return deferred.promise; | |
| 32 | + // } | |
| 35 | 33 | |
| 36 | 34 | /** |
| 37 | 35 | * Notify listeners that this article has been removed |
| 38 | 36 | */ |
| 39 | - private notifyArticleRemovedListeners(article: noosfero.Article) { | |
| 40 | - this.articleRemoved.next(article); | |
| 41 | - } | |
| 37 | + // private notifyArticleRemovedListeners(article: noosfero.Article) { | |
| 38 | + // this.modelRemovedEventEmitter.next(article); | |
| 39 | + // } | |
| 42 | 40 | |
| 43 | - /** | |
| 44 | - * subscribes to the ArticleRemoved event emitter | |
| 45 | - */ | |
| 46 | - subscribeToArticleRemoved(fn: Function) { | |
| 47 | - this.articleRemoved.subscribe(fn); | |
| 48 | - } | |
| 49 | 41 | |
| 50 | 42 | updateArticle(article: noosfero.Article) { |
| 51 | 43 | let headers = { | ... | ... |
src/lib/ng-noosfero-api/http/block.service.ts
| ... | ... | @@ -37,4 +37,14 @@ export class BlockService extends RestangularService<noosfero.Block> { |
| 37 | 37 | return apiContentPromise.promise; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | + getBlock<T extends noosfero.Block>(blockId: number): ng.IPromise<T> { | |
| 41 | + let deferred = this.$q.defer<T>(); | |
| 42 | + this.get(blockId) | |
| 43 | + .then((result: noosfero.RestResult<T>) => { | |
| 44 | + deferred.resolve(result.data); | |
| 45 | + }) | |
| 46 | + .catch(reason => deferred.reject(reason)); | |
| 47 | + return deferred.promise; | |
| 48 | + } | |
| 49 | + | |
| 40 | 50 | } | ... | ... |
src/lib/ng-noosfero-api/http/restangular_service.ts
| 1 | +import {EventEmitter} from "ng-forward"; | |
| 1 | 2 | /** |
| 2 | 3 | * @name RestangularService |
| 3 | 4 | * Base class to be extended by classes which will provide access |
| ... | ... | @@ -13,6 +14,11 @@ export abstract class RestangularService<T extends noosfero.RestModel> { |
| 13 | 14 | private baseResource: restangular.IElement; |
| 14 | 15 | private currentPromise: ng.IDeferred<T>; |
| 15 | 16 | |
| 17 | + protected modelFoundEventEmitter: EventEmitter<T> = new EventEmitter<any>(); | |
| 18 | + protected modelAddedEventEmitter: EventEmitter<T> = new EventEmitter<any>(); | |
| 19 | + protected modelRemovedEventEmitter: EventEmitter<T> = new EventEmitter<any>(); | |
| 20 | + protected modelUpdatedEventEmitter: EventEmitter<T> = new EventEmitter<any>(); | |
| 21 | + | |
| 16 | 22 | /** |
| 17 | 23 | * Creates an instance of RestangularService. |
| 18 | 24 | * |
| ... | ... | @@ -35,6 +41,22 @@ export abstract class RestangularService<T extends noosfero.RestModel> { |
| 35 | 41 | // }); |
| 36 | 42 | } |
| 37 | 43 | |
| 44 | + subscribeToModelRemoved(fn: ((model: T) => void)) { | |
| 45 | + this.modelRemovedEventEmitter.subscribe(fn); | |
| 46 | + } | |
| 47 | + | |
| 48 | + subscribeToModelAdded(fn: ((model: T) => void)) { | |
| 49 | + this.modelAddedEventEmitter.subscribe(fn); | |
| 50 | + } | |
| 51 | + | |
| 52 | + subscribeToModelUpdated(fn: ((model: T) => void)) { | |
| 53 | + this.modelUpdatedEventEmitter.subscribe(fn); | |
| 54 | + } | |
| 55 | + | |
| 56 | + subscribeToModelFound(fn: ((model: T) => void)) { | |
| 57 | + this.modelFoundEventEmitter.subscribe(fn); | |
| 58 | + } | |
| 59 | + | |
| 38 | 60 | public resetCurrent() { |
| 39 | 61 | this.currentPromise = this.$q.defer(); |
| 40 | 62 | } |
| ... | ... | @@ -107,7 +129,7 @@ export abstract class RestangularService<T extends noosfero.RestModel> { |
| 107 | 129 | restRequest = this.restangularService.one(this.getResourcePath(), id).get(queryParams, headers); |
| 108 | 130 | } |
| 109 | 131 | |
| 110 | - restRequest.then(this.getHandleSuccessFunction(deferred)) | |
| 132 | + restRequest.then(this.getHandleSuccessFunction(deferred, this.modelFoundEventEmitter)) | |
| 111 | 133 | .catch(this.getHandleErrorFunction(deferred)); |
| 112 | 134 | |
| 113 | 135 | |
| ... | ... | @@ -201,7 +223,7 @@ export abstract class RestangularService<T extends noosfero.RestModel> { |
| 201 | 223 | restRequest = restangularObj.remove(queryParams, headers); |
| 202 | 224 | |
| 203 | 225 | restRequest |
| 204 | - .then(this.getHandleSuccessFunction(deferred)) | |
| 226 | + .then(this.getHandleSuccessFunction(deferred, this.modelRemovedEventEmitter)) | |
| 205 | 227 | .catch(this.getHandleErrorFunction(deferred)); |
| 206 | 228 | |
| 207 | 229 | return deferred.promise; |
| ... | ... | @@ -226,7 +248,7 @@ export abstract class RestangularService<T extends noosfero.RestModel> { |
| 226 | 248 | |
| 227 | 249 | restRequest = restangularObj.put(queryParams, headers); |
| 228 | 250 | |
| 229 | - restRequest.then(this.getHandleSuccessFunction(deferred)) | |
| 251 | + restRequest.then(this.getHandleSuccessFunction(deferred, this.modelUpdatedEventEmitter)) | |
| 230 | 252 | .catch(this.getHandleErrorFunction(deferred)); |
| 231 | 253 | |
| 232 | 254 | return deferred.promise; |
| ... | ... | @@ -255,7 +277,7 @@ export abstract class RestangularService<T extends noosfero.RestModel> { |
| 255 | 277 | restRequest = this.baseResource.post(data, queryParams, headers); |
| 256 | 278 | } |
| 257 | 279 | |
| 258 | - restRequest.then(this.getHandleSuccessFunction(deferred)) | |
| 280 | + restRequest.then(this.getHandleSuccessFunction(deferred, this.modelAddedEventEmitter)) | |
| 259 | 281 | .catch(this.getHandleErrorFunction(deferred)); |
| 260 | 282 | |
| 261 | 283 | return deferred.promise; |
| ... | ... | @@ -289,7 +311,7 @@ export abstract class RestangularService<T extends noosfero.RestModel> { |
| 289 | 311 | } |
| 290 | 312 | |
| 291 | 313 | /** HANDLERS */ |
| 292 | - protected getHandleSuccessFunction<C>(deferred: ng.IDeferred<noosfero.RestResult<C | T | any>>, responseKey?: string): (response: restangular.IResponse) => void { | |
| 314 | + protected getHandleSuccessFunction<C>(deferred: ng.IDeferred<noosfero.RestResult<C | T | any>>, successEmitter: EventEmitter<T> = null): (response: restangular.IResponse) => void { | |
| 293 | 315 | let self = this; |
| 294 | 316 | |
| 295 | 317 | /** |
| ... | ... | @@ -301,7 +323,13 @@ export abstract class RestangularService<T extends noosfero.RestModel> { |
| 301 | 323 | if (self.$log) { |
| 302 | 324 | self.$log.debug("Request successfull executed", response.data, self, response); |
| 303 | 325 | } |
| 304 | - deferred.resolve(<any>this.extractData(response)); | |
| 326 | + let resultModel: noosfero.RestResult<T> = <any>this.extractData(response); | |
| 327 | + // resolve the promise with the model returned from the Noosfero API | |
| 328 | + deferred.resolve(resultModel); | |
| 329 | + // emits the event if a successEmiter was provided in the successEmitter parameter | |
| 330 | + if (successEmitter !== null) { | |
| 331 | + successEmitter.next(resultModel); | |
| 332 | + } | |
| 305 | 333 | }; |
| 306 | 334 | return successFunction; |
| 307 | 335 | } | ... | ... |