Hey!
I have a design question again. We need to pass a query param to our PUT route. This query param is called forceOverwrite and should be true/false if the data should overwrite existing data on the server. As I can not pass a parameter to the ember-data save method I think I have to extend ember-data somehow. I achieved it the following way (but I’m not sure if it is a good idea to do so): by the way I’m using ember-cli:
In the controller:
// ...
// determine if overwrite is true or false
// ...
self.get('model').save({
forceOverwrite: overwrite
}).then(function (res) {
// ...
}, function (err) {
// ...
});
In initializers/reopen-model.js
initialize: function () {
DS.Model.reopen({
save: function (queryParams) {
if (!Ember.isEmpty(queryParams)) {
this.set('queryParams', queryParams);
}
return this._super();
}
});
}
And in adapters/application.js
export default DS.RESTAdapter.extend({
// ....
buildURL: function (type, id, record) {
var url = this._super(type, id, record);
if (Ember.isEmpty(record)) {
return url;
}
var queryParams = Ember.get(record, 'queryParams');
if (!Ember.isEmpty(queryParams)) {
for (var key in queryParams) {
if (queryParams.hasOwnProperty(key)) {
var seperator = (url.indexOf('?') === -1) ? '?' : '&';
url += seperator + key + '=' + queryParams[key];
}
}
}
return url;
}
});
Is this approach good? I’m afraid that I’m running into some troubles I don’t see right now. Especially I’m not sure if it’s a good idea to append data to the record?
What do you think, is this solution good or could it be developed better?
Thanks for your opinions Bye