mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Create boundOneWay util function
Closes #2958 - Created `boundOneWay` util to extract common pattern of having a oneWay property that doesn't break its binding after being set - Changed `EditorControllerMixin.willPublish`, `PostSettingsMenuController.publishedAtValue` and `.slugValue` to use the new `boundOneWay` util
This commit is contained in:
parent
9112c1f469
commit
e3b302676b
3 changed files with 25 additions and 21 deletions
|
@ -1,6 +1,7 @@
|
||||||
/* global moment */
|
/* global moment */
|
||||||
import {parseDateString, formatDate} from 'ghost/utils/date-formatting';
|
import {parseDateString, formatDate} from 'ghost/utils/date-formatting';
|
||||||
import SlugGenerator from 'ghost/models/slug-generator';
|
import SlugGenerator from 'ghost/models/slug-generator';
|
||||||
|
import boundOneWay from 'ghost/utils/bound-one-way';
|
||||||
|
|
||||||
var PostSettingsMenuController = Ember.ObjectController.extend({
|
var PostSettingsMenuController = Ember.ObjectController.extend({
|
||||||
isStaticPage: function (key, val) {
|
isStaticPage: function (key, val) {
|
||||||
|
@ -29,21 +30,9 @@ var PostSettingsMenuController = Ember.ObjectController.extend({
|
||||||
}
|
}
|
||||||
return formatDate(moment());
|
return formatDate(moment());
|
||||||
}.property('publishedAtValue'),
|
}.property('publishedAtValue'),
|
||||||
|
publishedAtValue: boundOneWay('published_at', formatDate),
|
||||||
|
|
||||||
publishedAtValue: function (key, value) {
|
slugValue: boundOneWay('slug'),
|
||||||
if (arguments.length > 1) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
return formatDate(this.get('published_at'));
|
|
||||||
}.property('published_at'),
|
|
||||||
|
|
||||||
slugValue: function (key, value) {
|
|
||||||
if (arguments.length > 1) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
return this.get('slug');
|
|
||||||
}.property('slug'),
|
|
||||||
|
|
||||||
//Lazy load the slug generator for slugPlaceholder
|
//Lazy load the slug generator for slugPlaceholder
|
||||||
slugGenerator: Ember.computed(function () {
|
slugGenerator: Ember.computed(function () {
|
||||||
return SlugGenerator.create({ghostPaths: this.get('ghostPaths')});
|
return SlugGenerator.create({ghostPaths: this.get('ghostPaths')});
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
/* global console */
|
/* global console */
|
||||||
import MarkerManager from 'ghost/mixins/marker-manager';
|
import MarkerManager from 'ghost/mixins/marker-manager';
|
||||||
import PostModel from 'ghost/models/post';
|
import PostModel from 'ghost/models/post';
|
||||||
|
import boundOneWay from 'ghost/utils/bound-one-way';
|
||||||
|
|
||||||
// this array will hold properties we need to watch
|
// this array will hold properties we need to watch
|
||||||
// to know if the model has been changed (`controller.isDirty`)
|
// to know if the model has been changed (`controller.isDirty`)
|
||||||
|
@ -19,12 +20,7 @@ var EditorControllerMixin = Ember.Mixin.create(MarkerManager, {
|
||||||
* Only with a user-set value (via setSaveType action)
|
* Only with a user-set value (via setSaveType action)
|
||||||
* can the post's status change.
|
* can the post's status change.
|
||||||
*/
|
*/
|
||||||
willPublish: function (key, value) {
|
willPublish: boundOneWay('isPublished'),
|
||||||
if (arguments.length > 1) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
return this.get('isPublished');
|
|
||||||
}.property('isPublished'),
|
|
||||||
|
|
||||||
// set by the editor route and `isDirty`. useful when checking
|
// set by the editor route and `isDirty`. useful when checking
|
||||||
// whether the number of tags has changed for `isDirty`.
|
// whether the number of tags has changed for `isDirty`.
|
||||||
|
@ -120,7 +116,6 @@ var EditorControllerMixin = Ember.Mixin.create(MarkerManager, {
|
||||||
tags.removeObjects(oldTags);
|
tags.removeObjects(oldTags);
|
||||||
oldTags.invoke('deleteRecord');
|
oldTags.invoke('deleteRecord');
|
||||||
},
|
},
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
save: function () {
|
save: function () {
|
||||||
var status = this.get('willPublish') ? 'published' : 'draft',
|
var status = this.get('willPublish') ? 'published' : 'draft',
|
||||||
|
|
20
core/client/utils/bound-one-way.js
Normal file
20
core/client/utils/bound-one-way.js
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
/**
|
||||||
|
* Defines a property similarly to `Ember.computed.oneway`,
|
||||||
|
* save that while a `oneway` loses its binding upon being set,
|
||||||
|
* the `BoundOneWay` will continue to listen for upstream changes.
|
||||||
|
*
|
||||||
|
* This is an ideal tool for working with values inside of {{input}}
|
||||||
|
* elements.
|
||||||
|
* @param transform: a function to transform the **upstream** value.
|
||||||
|
*/
|
||||||
|
var BoundOneWay = function (upstream, transform) {
|
||||||
|
if (typeof transform !== 'function') {
|
||||||
|
//default to the identity function
|
||||||
|
transform = function (value) { return value; };
|
||||||
|
}
|
||||||
|
return function (key, value) {
|
||||||
|
return arguments.length > 1 ? value : transform(this.get(upstream));
|
||||||
|
}.property(upstream);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default BoundOneWay;
|
Loading…
Reference in a new issue