Defining buildURL depending on what properties have changed

617 views Asked by At

I would like to redefine my buildURL depending on what properties changed on the same model. For example, if the status changed, I would like to PUT to a certain route, and if the subuser changed, I would like to PUT to another route.

Example :

this.store.find('conversation', conv.id).then(function(conversation){
    conversation.set('status', 'opened');
    conversation.save();                    
});

This would use a certain PUT route and this :

this.store.find('conversation', this.get('selectedConv').id).then(function(conversation){
    conversation.set('subuser', subuser);
    conversation.set('url', subuser.get('email'));
    conversation.save();
});

And this would use another PUT route even tho the changes are made on the same model. This is all happening in a controller.

1

There are 1 answers

0
Andrey Mikhaylov - lolmaus On BEST ANSWER

You need to customize your conversation adapter, specifically the urlForUpdateRecord method.

The original method looks like this:

urlForUpdateRecord: function(id, modelName, snapshot) {
  return this._buildURL(modelName, id);
},

In this method, you need to examine the snapshot and adjust the URL accordingly.

The latest version of Ember Data has introduced the changedAttributes property. This seems to be what you need.

Good luck!