jwanglof
jwanglof

Reputation: 547

A subscription on a record doesn't fire when a nested value is changed

When I change a nested value on a record that is subscribed on the subscription isn't fired.

var recInfo = {};
recInfo.members = [];
recInfo.members.push({id: 1});
recInfo.members.push({id: 2});
recInfo.members.push({id: 3});

Example:

var record = client.record.getRecord('user/johan');

console.log(record.get());
Output:
{
  name: 'Johan',
  members: [
    {id: 1},
    {id: 2}
  ]
}

record.subscribe((data) => {
  log.debug(LOG_TAG + `Team '${model.name}' has changes. Changes:`, data);
});

Now if I update a user's value, e.g.:

record.set('members[1]', {id: 2, admin: true});

I expect that the record changes the member that have id 2 with the same id but adds the admin-value, and then fire the change-event that subscribe() should notice.

The change-part happens but it doesn't fire the change-event so the subscription is never fired.

Am I doing something wrong? The subscription works if I i.e. add a user.

EDIT:

So I found a way to make the change fire the subscription, and that is by cloning the members, make the change and then replace the entire members with the new values. However, the documents (http://deepstream.io/docs/record.html#set( path , value ) (sorry for the broken link)) states that I can do record.set( 'personalData.firstname', 'Marge' ); which will only update the firsname in personalData.

I know that I'm trying to change on an array's values, won't this work?

Can I only use set() to change on an object's value?

This is the workaround I'm using right now:

var members = lodash.clone(record.get('members'), true);
members[0].admin = true;
record.set('members', members);

Upvotes: 2

Views: 152

Answers (1)

yasserf
yasserf

Reputation: 391

I think the problem is due to using a different type of path notation.

record.set('members[1]', {id: 2, admin: true});

should work with:

record.set('members.1', {id: 2, admin: true});

The reason why is behind the scenes the json path used by deepstream doesn't differentiate between a normal js object and an array when trying to access something that exist.

Upvotes: 1

Related Questions