0

I have a service for a client's check_ins and a get function that returns the observable.

constructor(private clientService: ClientService) {
  this._check_ins = <BehaviorSubject<any>>new BehaviorSubject([]);
}

get check_ins() {
  return this._check_ins
}

updateCheckIns(check_ins) {
  this._check_ins.next(check_ins)
}

in my view I subscribe to the check_ins like so:

this.check_in_obs = this.checkInService.check_ins.subscribe(check_ins => {
      this.check_ins = check_ins
})

and check_in is the parent for another model, repositioning. When I delete the repositioning, I set it to null and attempt to update the check_in to show the repositioning is null:

this.check_in.repositioning = null
this.checkInService.updateCheckIns(this.check_ins)

This seems to work, and when I print the new value for this.check_ins, it is correct, however the view is not updated. I don't understand why the views value for this.check_ins is not in sync with the real value.

Here's the template code:

<ion-row class="" id="weigh-in-table" style="flex-wrap:nowrap;overflow-x: scroll;margin-left: 200px;margin-top:21px;">
  <ion-col
    no-padding
    style="min-width:300px;"
    *ngFor="let check_in of check_ins; let i = index"
  >
    <weigh
      [isDraggable]="dragEnabled"
      [check_in]="check_in"
      [check_ins] = "check_ins"
      [client_id]="client.id"
      [current_user]="current_user"
      [program_id]="program_id"
      [client]="client"
      [index]="i"
      [token]="current_user.auth_token"
      >
    </weigh>
  </ion-col>
</ion-row>
4
  • Could you please add your relevant template code as well? Commented Sep 25, 2017 at 17:34
  • I've updated the code Commented Sep 25, 2017 at 17:36
  • This get check_ins method isn't returning an observable, which may be preventing your subscription to it in your component from receiving the current value. Commented Sep 25, 2017 at 17:40
  • nope.. I don't get any error Commented Sep 25, 2017 at 17:44

1 Answer 1

1

Could you please try and modify your code to accommodate these changes and see if (it makes sense and if) it is working.

service

get check_ins() {
  return this._check_ins.asObservable();
}

component

this.check_ins = this.checkInService.check_ins;

// change this to
// this.check_in.repositioning = null
// this.checkInService.updateCheckIns(this.check_ins)
this.checkInService.updateCheckIns(null);
// if you still wanna make use of a this.check_in object please show what you do with it

template

<ion-row class="" id="weigh-in-table" style="flex-wrap:nowrap;overflow-x: scroll;margin-left: 200px;margin-top:21px;">
  <ion-col
    no-padding
    style="min-width:300px;"
    *ngFor="let check_in of check_ins | async; let i = index"
  >
    <weigh
      [isDraggable]="dragEnabled"
      [check_in]="check_in"
      [check_ins] = "check_ins | async"
      [client_id]="client.id"
      [current_user]="current_user"
      [program_id]="program_id"
      [client]="client"
      [index]="i"
      [token]="current_user.auth_token"
      >
    </weigh>
  </ion-col>
</ion-row>

Let me know if it made any difference.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm going to accept it as it gets most of the way there. Pointing out that I had been returning this._check_ins rather than this._check_ins.asObservable(); made a difference.
Cool, thanks for pointing that out. I was trying to force the object initialized as of type BehaviourSubject (which can be both an observer and an observable) to act as an observable by doing so. Was trying to figure out by mind than actually test it out, lol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.