Last active
February 24, 2017 12:07
-
-
Save emolr/a9d820a087dde3cc37b07d9c33cf224a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// article-form.service.ts | |
import {Injectable} from "@angular/core"; | |
import {AppFormsBase} from "../app-forms-base.class"; | |
import {ArticleFormBackend} from "./article-form.backend"; | |
import {ArticleFormModel} from "./article-form.model"; | |
import {IArticle} from "./article-form.interface"; | |
@Injectable() | |
export class ArticleFormService extends AppFormsBase { | |
public articleFormModel: ArticleFormModel; | |
constructor(public articleFormBackend: ArticleFormBackend) { | |
super(); | |
} | |
/*Here we are constructing our new form | |
with the data we provide it.*/ | |
public configure(data: IArticle) { | |
this.articleFormModel = new ArticleFormModel(data); | |
} | |
/*Request takes care of triggering our state variables from AppFormBase, | |
so they can be consumed in our template files.*/ | |
public request(method: string): Promise<any> { | |
this.isSendingSubject.next(true); | |
const formData = this.articleFormModel.form.getRawValue(); | |
let request: Promise<any>; | |
switch (method) { | |
case 'create': | |
request = this.articleFormBackend.create(formData); | |
break; | |
case 'update': | |
request = this.articleFormBackend.update(formData); | |
break; | |
case 'delete': | |
request = this.articleFormBackend.delete(formData); | |
break; | |
default: | |
console.log('Please provide a valid function name'); | |
} | |
return request.then( | |
res => { | |
this.isSendingSubject.next(false); | |
this.succeededSubject.next(true); | |
this.articleFormModel.form.reset(formData); | |
return res; | |
}, | |
err => { | |
this.failedSubject.next(err); | |
return err; | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment