mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-27 22:49:56 -05:00
Merge pull request #3423 from jaswilli/issue-3403
Add a mixin for saving a subset of a model.
This commit is contained in:
commit
6adc6efb3b
3 changed files with 131 additions and 8 deletions
|
@ -1,10 +1,11 @@
|
||||||
var TransferOwnerController = Ember.Controller.extend({
|
var TransferOwnerController = Ember.Controller.extend({
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
confirmAccept: function () {
|
confirmAccept: function () {
|
||||||
var user = this.get('model'),
|
var user = this.get('model'),
|
||||||
self = this;
|
self = this;
|
||||||
|
|
||||||
|
self.get('popover').closePopovers();
|
||||||
|
|
||||||
// Get owner role
|
// Get owner role
|
||||||
this.store.find('role').then(function (result) {
|
this.store.find('role').then(function (result) {
|
||||||
return result.findBy('name', 'Owner');
|
return result.findBy('name', 'Owner');
|
||||||
|
@ -12,16 +13,16 @@ var TransferOwnerController = Ember.Controller.extend({
|
||||||
// remove roles and assign owner role
|
// remove roles and assign owner role
|
||||||
user.get('roles').clear();
|
user.get('roles').clear();
|
||||||
user.get('roles').pushObject(ownerRole);
|
user.get('roles').pushObject(ownerRole);
|
||||||
return user.save({ format: false });
|
|
||||||
}).then(function (model) {
|
return user.saveOnly('roles');
|
||||||
|
}).then(function () {
|
||||||
self.notifications.closePassive();
|
self.notifications.closePassive();
|
||||||
self.notifications.showSuccess('Settings successfully saved.');
|
self.notifications.showSuccess('Ownership successfully transferred to ' + user.get('name'));
|
||||||
return model;
|
|
||||||
}).catch(function (errors) {
|
}).catch(function (errors) {
|
||||||
self.notifications.closePassive();
|
self.notifications.closePassive();
|
||||||
|
|
||||||
|
errors = Ember.isArray(errors) ? errors : Ember.A([errors]);
|
||||||
self.notifications.showErrors(errors);
|
self.notifications.showErrors(errors);
|
||||||
}).finally(function () {
|
|
||||||
self.get('popover').closePopovers();
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
121
core/client/mixins/selective-save.js
Normal file
121
core/client/mixins/selective-save.js
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
// SelectiveSaveMixin adds a saveOnly method to a DS.Model.
|
||||||
|
//
|
||||||
|
// saveOnly provides a way to save one or more properties of a model while
|
||||||
|
// preserving outstanding changes to other properties.
|
||||||
|
var SelectiveSaveMixin = Ember.Mixin.create({
|
||||||
|
saveOnly: function () {
|
||||||
|
if (arguments.length === 0) {
|
||||||
|
return Ember.RSVP.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arguments.length === 1 && Ember.isArray(arguments[0])) {
|
||||||
|
return this.saveOnly.apply(this, Array.prototype.slice.call(arguments[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
var propertiesToSave = Array.prototype.slice.call(arguments),
|
||||||
|
changed,
|
||||||
|
hasMany = {},
|
||||||
|
belongsTo = {},
|
||||||
|
self = this;
|
||||||
|
|
||||||
|
changed = this.changedAttributes();
|
||||||
|
|
||||||
|
// disable observers so we can make changes to the model but not have
|
||||||
|
// them reflected by the UI
|
||||||
|
this.beginPropertyChanges();
|
||||||
|
|
||||||
|
// make a copy of any relations the model may have so they can
|
||||||
|
// be reapplied later
|
||||||
|
this.eachRelationship(function (name, meta) {
|
||||||
|
if (meta.kind === 'hasMany') {
|
||||||
|
hasMany[name] = self.get(name).slice();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (meta.kind === 'belongsTo') {
|
||||||
|
belongsTo[name] = self.get(name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
// roll back all changes to the model and then reapply only those that
|
||||||
|
// are part of the saveOnly
|
||||||
|
|
||||||
|
self.rollback();
|
||||||
|
|
||||||
|
propertiesToSave.forEach(function (name) {
|
||||||
|
if (hasMany.hasOwnProperty(name)) {
|
||||||
|
self.get(name).clear();
|
||||||
|
|
||||||
|
hasMany[name].forEach(function (relatedType) {
|
||||||
|
self.get(name).pushObject(relatedType);
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (belongsTo.hasOwnProperty(name)) {
|
||||||
|
return self.updateBelongsTo(name, belongsTo[name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changed.hasOwnProperty(name)) {
|
||||||
|
return self.set(name, changed[name][1]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
// if we were not able to get the model into the correct state
|
||||||
|
// put it back the way we found it and return a rejected promise
|
||||||
|
|
||||||
|
Ember.keys(changed).forEach(function (name) {
|
||||||
|
self.set(name, changed[name][1]);
|
||||||
|
});
|
||||||
|
|
||||||
|
Ember.keys(hasMany).forEach(function (name) {
|
||||||
|
self.updateHasMany(name, hasMany[name]);
|
||||||
|
});
|
||||||
|
|
||||||
|
Ember.keys(belongsTo).forEach(function (name) {
|
||||||
|
self.updateBelongsTo(name, belongsTo[name]);
|
||||||
|
});
|
||||||
|
|
||||||
|
self.endPropertyChanges();
|
||||||
|
|
||||||
|
return Ember.RSVP.reject(new Error(err.message || 'Error during saveOnly. Changes NOT saved.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.save().finally(function () {
|
||||||
|
// reapply any changes that were not part of the save
|
||||||
|
|
||||||
|
Ember.keys(changed).forEach(function (name) {
|
||||||
|
if (propertiesToSave.hasOwnProperty(name)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.set(name, changed[name][1]);
|
||||||
|
});
|
||||||
|
|
||||||
|
Ember.keys(hasMany).forEach(function (name) {
|
||||||
|
if (propertiesToSave.hasOwnProperty(name)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.updateHasMany(name, hasMany[name]);
|
||||||
|
});
|
||||||
|
|
||||||
|
Ember.keys(belongsTo).forEach(function (name) {
|
||||||
|
if (propertiesToSave.hasOwnProperty(name)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.updateBelongsTo(name, belongsTo[name]);
|
||||||
|
});
|
||||||
|
|
||||||
|
// signal that we're finished and normal model observation may continue
|
||||||
|
self.endPropertyChanges();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default SelectiveSaveMixin;
|
|
@ -1,7 +1,8 @@
|
||||||
import ValidationEngine from 'ghost/mixins/validation-engine';
|
import ValidationEngine from 'ghost/mixins/validation-engine';
|
||||||
import NProgressSaveMixin from 'ghost/mixins/nprogress-save';
|
import NProgressSaveMixin from 'ghost/mixins/nprogress-save';
|
||||||
|
import SelectiveSaveMixin from 'ghost/mixins/selective-save';
|
||||||
|
|
||||||
var User = DS.Model.extend(NProgressSaveMixin, ValidationEngine, {
|
var User = DS.Model.extend(NProgressSaveMixin, SelectiveSaveMixin, ValidationEngine, {
|
||||||
validationType: 'user',
|
validationType: 'user',
|
||||||
|
|
||||||
uuid: DS.attr('string'),
|
uuid: DS.attr('string'),
|
||||||
|
|
Loading…
Add table
Reference in a new issue