commit 43f35afc20b77bca4b3645b8f8d4f42b9f5f3104
parent a7bbe56a57c291ef76a475cbf2c80aeb10564522
Author: Konstantin Tarkus <hello@tarkus.me>
Date: Thu, 13 Nov 2014 12:31:41 +0300
Refactor PageStore, PageActions
Diffstat:
9 files changed, 44 insertions(+), 74 deletions(-)
diff --git a/gulpfile.js b/gulpfile.js
@@ -109,24 +109,22 @@ gulp.task('images', function() {
gulp.task('pages', function() {
src.pages = ['src/pages/**/*.js', 'src/pages/404.html'];
- var data = {};
+ var currentPage = {};
var ActionTypes = require('./src/constants/ActionTypes');
var AppDispatcher = require('./src/AppDispatcher');
- // Capture document.title changes
+ // Capture document.title and other page metadata changes
AppDispatcher.register(function(payload) {
- switch (payload.action.actionType)
+ if (payload.action.actionType == ActionTypes.SET_CURRENT_PAGE)
{
- case ActionTypes.SET_PAGE_TITLE:
- data.title = payload.action.text;
- break;
+ currentPage = payload.action.page;
}
return true;
});
var render = $.render({
template: './src/pages/_template.html',
- data: function() { return data; }
+ data: function() { return currentPage; }
})
.on('error', function(err) { console.log(err); render.end(); });
diff --git a/src/actions/PageActions.js b/src/actions/PageActions.js
@@ -10,42 +10,20 @@
var AppDispatcher = require('../AppDispatcher');
var ActionTypes = require('../constants/ActionTypes');
+var pageDefaults = require('../constants/Settings').defaults.page;
+var assign = require('object-assign');
-var PageActions = {
+module.exports = {
/**
- * Set a title for the current page.
- * @param {string} text The text to be set as a page title.
+ * Set metadata for the current page (title, description, keywords etc.).
+ * @param {object} The page object.
*/
- setTitle(text) {
+ set(page) {
AppDispatcher.handleViewAction({
- actionType: ActionTypes.SET_PAGE_TITLE,
- text: text
- });
- },
-
- /**
- * Set description for the current page.
- * @param {string} text The text to be set as a page description.
- */
- setDescription(text) {
- AppDispatcher.handleViewAction({
- actionType: ActionTypes.SET_PAGE_DESC,
- text: text
- });
- },
-
- /**
- * Set keywords for the current page.
- * @param {string} text The text to be set as page keywords.
- */
- setKeywords(text) {
- AppDispatcher.handleViewAction({
- actionType: ActionTypes.SET_PAGE_KEYWORDS,
- text: text
+ actionType: ActionTypes.SET_CURRENT_PAGE,
+ page: assign({}, pageDefaults, page)
});
}
};
-
-module.exports = PageActions;
diff --git a/src/actions/RouteActions.js b/src/actions/RouteActions.js
@@ -11,7 +11,7 @@
var AppDispatcher = require('../AppDispatcher');
var ActionTypes = require('../constants/ActionTypes');
-var AppActions = {
+module.exports = {
/**
* Set the current route.
@@ -25,5 +25,3 @@ var AppActions = {
}
};
-
-module.exports = AppActions;
diff --git a/src/constants/ActionTypes.js b/src/constants/ActionTypes.js
@@ -12,8 +12,6 @@ module.exports = {
SET_CURRENT_ROUTE: 'SET_CURRENT_ROUTE',
// Page action types
- SET_PAGE_TITLE: 'SET_PAGE_TITLE',
- SET_PAGE_DESC: 'SET_PAGE_DESC',
- SET_PAGE_KEYWORDS: 'SET_PAGE_KEYWORDS'
+ SET_CURRENT_PAGE: 'SET_CURRENT_PAGE'
};
diff --git a/src/constants/Settings.js b/src/constants/Settings.js
@@ -8,8 +8,14 @@
module.exports = {
- title: 'React.js Starter Kit',
- description: 'A skeleton for an isomorphic web application (SPA) built with React.js and Flux',
- keywords: null
+ defaults: {
+
+ page: {
+ title: 'React.js Starter Kit',
+ description: 'A skeleton for an isomorphic web application (SPA) built with React.js and Flux',
+ keywords: null
+
+ }
+ }
};
diff --git a/src/layouts/DefaultLayout.js b/src/layouts/DefaultLayout.js
@@ -19,7 +19,7 @@ var Navbar = require('../components/Navbar');
*/
function getState() {
return {
- title: PageStore.getCurrentPage().title
+ title: PageStore.get().title
};
}
@@ -31,7 +31,7 @@ var DefaultLayout = React.createClass({
componentDidMount() {
PageStore.addEventListener(this._onChange);
- this._onChange();
+ PageStore.emitChange();
},
componentWillUnmount() {
diff --git a/src/pages/Index.js b/src/pages/Index.js
@@ -19,7 +19,7 @@ var HomePage = React.createClass({
},
componentWillMount() {
- PageActions.setTitle('React.js Starter Kit');
+ PageActions.set({title: 'React.js Starter Kit'});
},
render() {
diff --git a/src/pages/Privacy.js b/src/pages/Privacy.js
@@ -26,7 +26,7 @@ var PrivacyPage = React.createClass({
},
componentWillMount() {
- PageActions.setTitle('Privacy Policy');
+ PageActions.set({title: 'Privacy Policy'});
},
render() {
diff --git a/src/stores/PageStore.js b/src/stores/PageStore.js
@@ -10,30 +10,30 @@
var AppDispatcher = require('../AppDispatcher');
var ActionTypes = require('../constants/ActionTypes');
-var Settings = require('../constants/Settings');
var EventEmitter = require('events').EventEmitter;
var assign = require('object-assign');
var CHANGE_EVENT = 'change';
-var page = {
- title: null,
- description: null,
- keywords: null
-};
+/**
+ * @typedef Page
+ * @type {object}
+ * @property {string} title
+ * @property {string} description
+ * @property {string} keywords
+ */
+
+/** @type {Page} */
+var _page;
var PageStore = assign({}, EventEmitter.prototype, {
/**
- * Get metadata associated with the current page.
- * @returns {object}
+ * Get the current page.
+ * @returns {Page}
*/
- getCurrentPage() {
- return {
- title: page.title || Settings.title,
- description: page.description || Settings.description,
- keywords: page.keywords || Settings.keywords
- };
+ get() {
+ return _page || require('../constants/Settings').defaults.page;
},
emitChange() {
@@ -57,16 +57,8 @@ AppDispatcher.register(function(payload) {
switch (action.actionType)
{
- case ActionTypes.SET_PAGE_TITLE:
- page.title = action.text;
- break;
-
- case ActionTypes.SET_PAGE_DESC:
- page.description = action.description;
- break;
-
- case ActionTypes.SET_PAGE_KEYWORDS:
- page.keywords = action.keywords;
+ case ActionTypes.SET_CURRENT_PAGE:
+ _page = action.page;
break;
default: