Dry up tag saving behavior

Ref #4248
This commit is contained in:
Matt Enlow 2014-12-09 07:25:20 -07:00
parent 93c5ff3e04
commit a04157daeb

View File

@ -9,19 +9,42 @@ var TagsController = Ember.ArrayController.extend(PaginationMixin, {
activeTagSlugScratch: boundOneWay('activeTag.slug'), activeTagSlugScratch: boundOneWay('activeTag.slug'),
activeTagDescriptionScratch: boundOneWay('activeTag.description'), activeTagDescriptionScratch: boundOneWay('activeTag.description'),
// Tag properties that should not be set to the empty string
requiredTagProperties: ['name', 'slug'],
init: function (options) { init: function (options) {
options = options || {}; options = options || {};
options.modelType = 'tag'; options.modelType = 'tag';
this._super(options); this._super(options);
}, },
saveActiveTag: function () { saveActiveTagProperty: function (propKey, newValue) {
var activeTag = this.get('activeTag'), var activeTag = this.get('activeTag'),
name = activeTag.get('name'), currentValue = activeTag.get(propKey),
self = this; requiredTagProps = this.get('requiredTagProperties'),
self = this,
tagName;
newValue = newValue.trim();
// Quit if value is empty for a required property
if (!newValue && requiredTagProps.contains(propKey)) {
return;
}
// Quit if there was no change
if (newValue === currentValue) {
return;
}
activeTag.set(propKey, newValue);
tagName = activeTag.get('name');
// don't save a new tag until it has a name
if (!tagName) {
return;
}
activeTag.save().then(function () { activeTag.save().then(function () {
self.notifications.showSuccess('Saved ' + name); self.notifications.showSuccess('Saved ' + tagName);
}).catch(function (error) { }).catch(function (error) {
self.notifications.showAPIError(error); self.notifications.showAPIError(error);
}); });
@ -52,47 +75,15 @@ var TagsController = Ember.ArrayController.extend(PaginationMixin, {
}, },
saveActiveTagName: function (name) { saveActiveTagName: function (name) {
var activeTag = this.get('activeTag'), this.saveActiveTagProperty('name', name);
currentName = activeTag.get('name');
name = name.trim();
if (!name || name === currentName) {
return;
}
// setting all of the properties as they are not saved until name is present
activeTag.setProperties({
name: name,
slug: this.get('activeTagSlugScratch').trim(),
description: this.get('activeTagDescriptionScratch').trim()
});
this.saveActiveTag();
}, },
saveActiveTagSlug: function (slug) { saveActiveTagSlug: function (slug) {
var name = this.get('activeTag.name'), this.saveActiveTagProperty('slug', slug);
currentSlug = this.get('activeTag.slug') || '';
slug = slug.trim();
if (!name || !slug || slug === currentSlug) {
return;
}
this.set('activeTag.slug', slug);
this.saveActiveTag();
}, },
saveActiveTagDescription: function (description) { saveActiveTagDescription: function (description) {
var name = this.get('activeTag.name'), this.saveActiveTagProperty('description', description);
currentDescription = this.get('activeTag.description') || '';
description = description.trim();
if (!name || description === currentDescription) {
return;
}
this.set('activeTag.description', description);
this.saveActiveTag();
} }
} }
}); });