FIX: If a topic title edit fails, revert to previous title.

This commit is contained in:
Robin Ward 2014-07-21 13:39:23 -04:00
parent 0f23456259
commit d9e5c2317f
6 changed files with 17 additions and 71 deletions

View File

@ -313,11 +313,11 @@ Discourse.AdminUser = Discourse.User.extend({
} else {
bootbox.alert(I18n.t("admin.user.delete_failed"));
if (data.user) {
user.mergeAttributes(data.user);
user.setProperties(data.user);
}
}
}, function() {
Discourse.AdminUser.find( user.get('username') ).then(function(u){ user.mergeAttributes(u); });
Discourse.AdminUser.find( user.get('username') ).then(function(u){ user.setProperties(u); });
bootbox.alert(I18n.t("admin.user.delete_failed"));
});
};

View File

@ -153,7 +153,7 @@ Discourse.Report.reopenClass({
row.percentage = Math.round((row.y / maxY) * 100);
});
}
model.mergeAttributes(json.report);
model.setProperties(json.report);
model.set('loaded', true);
});
return(model);

View File

@ -214,7 +214,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
this.set('topicSaving', true);
// manually update the titles & category
topic.setProperties({
var backup = topic.setPropertiesBackup({
title: this.get('newTitle'),
category_id: parseInt(this.get('newCategoryId'), 10),
fancy_title: this.get('newTitle')
@ -224,16 +224,11 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
var self = this;
topic.save().then(function(result){
// update the title if it has been changed (cleaned up) server-side
var title = result.basic_topic.title;
var fancy_title = result.basic_topic.fancy_title;
topic.setProperties({
title: title,
fancy_title: fancy_title
});
topic.setProperties(Em.getProperties(result.basic_topic, 'title', 'fancy_title'));
self.set('topicSaving', false);
}, function(error) {
self.set('editingTopic', true);
self.set('topicSaving', false);
self.setProperties({ editingTopic: true, topicSaving: false });
topic.setProperties(backup);
if (error && error.responseText) {
bootbox.alert($.parseJSON(error.responseText).errors[0]);
} else {

View File

@ -1,40 +1,19 @@
/**
A base object we can use to handle models in the Discourse client application.
@class Model
@extends Ember.Object
@uses Discourse.Presence
@namespace Discourse
@module Discourse
**/
Discourse.Model = Ember.Object.extend(Discourse.Presence, {
/**
Update our object from another object
@method mergeAttributes
@param {Object} attrs The attributes we want to merge with
**/
mergeAttributes: function(attrs) {
var self = this;
_.each(attrs, function(v, k) {
self.set(k, v);
});
// Like `setProperties` but returns the original values in case
// we want to roll back
setPropertiesBackup: function(obj) {
var backup = this.getProperties(Ember.keys(obj));
this.setProperties(obj);
return backup;
}
});
Discourse.Model.reopenClass({
/**
Given an array of values, return them in a hash
@method extractByKey
@param {Object} collection The collection of values
@param {Object} klass The class to instantiate
**/
extractByKey: function(collection, klass) {
var retval = {};
_.each(collection, function(item) {
if (Ember.isEmpty(collection)) { return retval; }
collection.forEach(function(item) {
retval[item.id] = klass.create(item);
});
return retval;

View File

@ -64,7 +64,7 @@ Discourse.Site = Discourse.Model.extend({
updateCategory: function(newCategory) {
var existingCategory = this.get('categories').findProperty('id', Em.get(newCategory, 'id'));
if (existingCategory) existingCategory.mergeAttributes(newCategory);
if (existingCategory) existingCategory.setProperties(newCategory);
}
});

View File

@ -4,34 +4,6 @@ test("mixes in Discourse.Presence", function() {
ok(Discourse.Presence.detect(Discourse.Model.create()));
});
test("mergeAttributes: merges attributes from another object", function() {
var model = Discourse.Model.create({
foo: "foo",
bar: "original bar"
});
model.mergeAttributes({
bar: "merged bar",
baz: "baz"
});
equal(model.get("foo"), "foo", "leaving original attr intact when only original object contains given key");
equal(model.get("bar"), "merged bar", "overwriting original attr when both objects contain given key");
equal(model.get("baz"), "baz", "adding new attr to original object when only merged object contains given key");
});
test("mergeAttributes: respects Ember setters (so observers etc. work)", function() {
var observerHasFired = false;
var model = Discourse.Model.create({foo: "original foo"});
model.addObserver("foo", function() {
observerHasFired = true;
});
model.mergeAttributes({foo: "merged foo"});
ok(observerHasFired);
});
test("extractByKey: converts a list of hashes into a hash of instances of specified class, indexed by their ids", function() {
var firstObject = {id: "id_1", foo: "foo_1"};
var secondObject = {id: "id_2", foo: "foo_2"};