
ليه الـ httpResource مش مناسب للـ POST والـ PUT؟
- كاتب موثّق
- Angular 22
محتويات المقال
httpResource() نضّف كود التحميل، فعمل الـ Save بنفس الطريقة: PUT /api/profile والـ body بتاعه هو الـ draft signal بتاع الفورم. في الـ Network panel، الصفحة بتبعت PUT أول ما تفتح، قبل ما حد يكتب حرف، وجواه الـ draft الفاضي. وبعدين تبعت واحد مع كل ضغطة زرار، ومعظمهم بيظهروا canceled. بس الـ logs بتاعة الـ backend بتحكي حكاية تانية: كذا request من اللي اتلغوا وصلوا واتحفظوا، وأولهم مسح الـ bio بتاع اليوزر.
الفكرة الغلط الشائعة
httpResource() هو البديل بتاع عصر الـ signals لـ HttpClient، فكل call لازم يبقى resource، حتى الـ POST والـ PUT اللي بيحفظوا داتا. وAPI واحد لكل حاجة شكله أنضف من إنك تخلط Observables وsignals في نفس الـ component.
method وbody، فالـ PUT بيعدّي من الـ type check وبيشتغل. والـ resource مبني على HttpClient، فالـ interceptors والـ auth headers بتشتغل عادي. وبقى stable في Angular 22، وأول اختبار يدوي بينجح: تكتب، الـ request يطلع، الداتا تتحفظ. ومفيش حاجة في الـ API بتقول "للقراية بس". والقواعد اللي بتخليه مش مناسب للكتابة هي نفسها اللي بتخليه مريح جدًا في القراية، وعشان كده بتعدّي من غير ما حد ياخد باله.
اللي بيحصل فعلًا
الـ resource بيوصف داتا، مش action
بيبعت من غير ما يستنى حد
HttpClient بيرجّع : مفيش حاجة بتطلع لحد ما حد يعمل subscribe. والـ guide بتاع httpResource بيوضح الفرق: هو "initiates the request eagerly". يعني resource فيه method: 'PUT' بيبعت الـ PUT بتاعه أول ما يتعمل، بأي body موجود في الـ signals ساعتها. وفي الحكاية اللي فوق، ده كان draft فاضي، اتبعت قبل حتى ما الـ profile يحمّل.
كل signal بتقراها بقت trigger
computed() بالظبط. والـ guide بيقول إن كل ما signal هو معتمد عليها تتغير، الـ resource "will emit a new http request". وده بيشمل الـ body. اقرا this.draft() هناك، وكل ضغطة زرار بقت save. اقرا locale signal عشان header، وتغيير اللغة هيعمل save تاني. ممكن تركّب signal اسمها "submitted" وترجّع undefined لحد الضغطة، بس ساعتها انت بتبني click handler من حتت reactive، وبرضه هتورث القاعدة اللي جاية.
الإلغاء صح في القراية وخطر في الكتابة
switchMap جاهز جوه، ومش هتقدر تبدّله بإنك تعمل queue أو تتجاهل الضغطة التانية.
reload() برضه مش مناسب. في الـ resource معناه "هات ده تاني"، وفي PUT معناه "احفظ تاني".
اقرا بـ resource، واكتب بـ HttpClient
httpResource صريح: "Avoid using httpResource for mutations like POST or PUT. Instead, prefer directly using the underlying HttpClient APIs." خلّي الـ resource للداتا اللي الصفحة بتعرضها، وابعت الـ save من الـ event handler، وبعدين قول للـ resource إيه اللي اتغير.
import { httpResource } from '@angular/common/http';import { Component, signal } from '@angular/core';@Component({ selector: 'app-profile-form', template: `<input [value]="draft().bio" (input)="edit($event)" />`,})export class ProfileForm { readonly draft = signal<Profile>({ name: '', bio: '' }); // Sends a PUT on creation with the empty draft, then one per // keystroke; each new PUT cancels the one still pending readonly save = httpResource<Profile>(() => ({ url: '/api/profile', method: 'PUT', body: this.draft(), })); edit(event: Event) { const bio = (event.target as HTMLInputElement).value; this.draft.update((d) => ({ ...d, bio })); }}import { HttpClient, httpResource } from '@angular/common/http';import { Component, inject, signal } from '@angular/core';import { firstValueFrom } from 'rxjs';@Component({ selector: 'app-profile-form', template: `<button (click)="save()" [disabled]="saving()">Save</button>`,})export class ProfileForm { private readonly http = inject(HttpClient); // Reading: one GET when created; reload() fetches it again readonly profile = httpResource<Profile>(() => '/api/profile'); readonly draft = signal<Profile>({ name: '', bio: '' }); readonly saving = signal(false); // Writing: one PUT per click, sent now, and nothing cancels it async save() { this.saving.set(true); try { const req = this.http.put<Profile>('/api/profile', this.draft()); this.profile.set(await firstValueFrom(req)); // status(): 'local' } finally { this.saving.set(false); } }}PUT موجود جوه save() بس، فبيتبعت مرة واحدة مع كل ضغطة، ولو فشل الـ await بيرمي error تقدر تتعامل معاه، بدل ما يضيع في request اتلغى. والـ saving signal بتقفل الزرار، فالـ double click المستعجل مايقدرش يبعت اتنين save. وبعد النجاح، set() بتحط رد الـ server في الـ resource، والـ status بتاعه بيبقى local، ودي الحالة اللي الـ guide بيستخدمها لقيمة اتحطت محليًا مش اتحمّلت.
POST اللي بيقرا بس، زي search endpoint بياخد الـ filters في الـ body أو GraphQL query، بيتصرف زي القراية وينفع يبقى resource. وأي request بيغيّر حاجة على الـ server مكانه الـ event handler، أيًا كان الـ verb بتاعه.
httpResource() | HttpClient في handler | |
|---|---|---|
await firstValueFrom() | ||
reload() | ||
value() signal بتاعه | ||
تطبقها إزاي في مشروعك
دوّر على الـ resources اللي بتكتب دوّر على httpResource(وrxResource(وresource(، وشوف أيmethodغيرGET، أو loader بيناديpostأوputأوpatchأوdelete.انقل كل كتابة لـ method نادي HttpClientمن الـ event handler، زي(click)أو(ngSubmit)، بـsubscribe()أوfirstValueFrom()، عشان الـ request مايطلعش غير لما اليوزر يعمل حاجة.ظبّط ناحية القراية بعد save ناجح، اعمل set()بالـ entity اللي رجعت أوreload()للـ resource. ولو فشل، سيب الـ resource زي ما هو واعرض الـ error جنب الفورم.امنع الـ submit المتكرر اقفل الزرار طول ما الـ savingsignal بـ true، أو عدّي الضغطات علىexhaustMapلو الـ save أصلًا جوه RxJS stream.
الخلاصة
الـ httpResource()بيبعت بدري، ويبعت تاني لما أي signal بيقراها تتغير، ويلغي الـ request اللي لسه شغال.دي قواعد قراية. والـ resource اللي فيه PUTبيعمل save أول ما يتعمل ومع كل تغيير في الـ body.إلغاء الكتابة بيوقّف المتصفح عن الانتظار بس، والـ server ممكن يكون حفظها خلاص. حمّل بـ httpResource()، واعمل الـ save بـHttpClientمن الـ event handler، وبعدينset()أوreload()للـ resource.
التعليقات (0)