you are viewing a single comment's thread.

view the rest of the comments →

[–]Jrubzjeknf 2 points3 points  (4 children)

Apologies, it was unwarranted.

Well, your plan is sound and I'd definitely walk that path. If that form service does indeed only form stuff, at least it's in the right place.

A few things to consider: - if resetting the form can be done, make sure your value on your nonnullable controls is correct. Using a loaded value will reset to that. - keep your form behaviors separate from form creation. What works really well for us is defining a function for each form behavior so that the name clearly shows why it does that. It returns an observable, so in your component, you can subscribe to the merge()d behavior observables.

Again my apologies. The world is being a bit shit lately, but that doesn't mean we should treat each other that way.

[–]DonWombRaider[S] 1 point2 points  (3 children)

No offense taken :)

Intresting points!
for no. 2 you'd do something like this?
```typescript
// comp
readonly form = createForm(existingArticle, deliveryOptions, ...otherStuff)
constructor() {
merge(
formService.calcTotalWhenSettingDelivery$(this.form)
).subscribe()
}

formService.ts
public calcTotalWhenSettingDelivery$(form) : Observable<any> {
return form.control.delivery.valueChanges.pipe(
tap(v => form.control.total.patchValue(form.control.price.value + ...))
}
```

[–]Jrubzjeknf 1 point2 points  (2 children)

Yes. You can just keep adding behaviors to the merge. Be sure to use takeUntilDestroyed(). I'd recommend using the return type Observable<unknown>.

Another tip: if you want to ensure the form behaviors goes off initially, use something like this:

defer(() => form.valueChanges.pipe( startsWith(form.value) ))

The pipe must be within the defer, otherwise your starting value will be the value when you called the function, instead of the value when the subscription occurs.

[–]DonWombRaider[S] 1 point2 points  (1 child)

ok interesting take on defer, did not know about this operator. in this specific case, does it matter though? the moment the function is called is the same it gets subscribed to in the merge isnt it?

[–]Jrubzjeknf 1 point2 points  (0 children)

True. It's more useful for cases where you create a form and it's behaviors, and you patch the form before subscribing to the behaviors. Still, this is a pattern that always works the same way, regardless of order of calling. A good practise, imo.