Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from newyork-anthonyng/feature/an/server-side-r…
…endering Include an example of server-side rendering
- Loading branch information
Showing
9 changed files
with
216 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import store from '../store'; | ||
|
||
class { | ||
onCreate(input, out) { | ||
// out.global is received from the $global object from route.js | ||
this.PRELOADED_STATE = out.global.PRELOADED_STATE; | ||
// create redux store on the server-side | ||
this.reduxStore = store.initialize(this.PRELOADED_STATE); | ||
} | ||
onMount() { | ||
// recreate redux store on the client-side | ||
this.reduxStore = store.initialize(this.PRELOADED_STATE); | ||
this.reduxStore.subscribe(() => { | ||
this.forceUpdate(); | ||
}); | ||
} | ||
dispatch(type) { | ||
this.reduxStore.dispatch({ type: type }); | ||
} | ||
} | ||
|
||
$ var reduxState = {}; | ||
/* | ||
* On the client-side, Marko first does a "lightweight re-render" of the UI | ||
* component tree before the `onMount` event is fired. The DOM is not actually | ||
* updated in this "lightweight re-render". | ||
* We check for the existence of `this.reduxStore` because it is first created in | ||
* the `onMount` event. | ||
*/ | ||
$ if(typeof component.reduxStore.getState === 'function') { | ||
reduxState = component.reduxStore.getState(); | ||
} | ||
|
||
<counter | ||
value=reduxState.value | ||
on-increment('dispatch', 'INCREMENT') | ||
on-decrement('dispatch', 'DECREMENT') /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* This is a presentation component that has no knowledge of Redux. | ||
* When an action needs to be performed it simply emits an event | ||
* that needs to be handled by a parent container component. | ||
* In this example, the `app` component will handle the `increment` and | ||
* `decrement` actions by directly interacting with the Redux store. | ||
*/ | ||
class { | ||
increment() { | ||
this.emit('increment'); | ||
} | ||
decrement() { | ||
this.emit('decrement'); | ||
} | ||
incrementIfOdd() { | ||
if (this.input.value % 2 !== 0) { | ||
this.increment(); | ||
} | ||
} | ||
incrementAsync() { | ||
setTimeout(() => { | ||
this.increment(); | ||
}, 1000); | ||
} | ||
} | ||
|
||
<div> | ||
Current count: ${input.value} | ||
<p> | ||
<button on-click('increment')>+</button> | ||
<button on-click('decrement')>-</button> | ||
<button on-click('incrementIfOdd')> | ||
Increment if odd | ||
</button> | ||
<button on-click('incrementAsync')> | ||
Increment async | ||
</button> | ||
</p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Marko + Redux</title> | ||
|
||
<!-- CSS includes --> | ||
<lasso-head/> | ||
</head> | ||
<body> | ||
<!-- Top-level UI component: --> | ||
<app/> | ||
|
||
<!-- JS includes --> | ||
<lasso-body/> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module.exports = function(state, action) { | ||
state = state || { | ||
value: 0 | ||
}; | ||
|
||
switch (action.type) { | ||
case 'INCREMENT': | ||
return { | ||
value: state.value + 1 | ||
}; | ||
case 'DECREMENT': | ||
return { | ||
value: state.value - 1 | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var view = require('./index'); | ||
|
||
module.exports = function(req, res) { | ||
/* | ||
* We can create the initial state of the redux store on the server. | ||
* In this example, the initial state of our store is retrieved from | ||
* the request parameters. | ||
*/ | ||
var value = parseInt(req.params.value); | ||
|
||
res.marko(view, { | ||
$global: { | ||
PRELOADED_STATE: { | ||
value: value, | ||
}, | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var redux = require('redux'); | ||
var counter = require('./reducers'); | ||
|
||
/* | ||
* We use an Immediately-Invoked Function Expression (IIFE) to hold our | ||
* redux store. This allows us to create a store client-side using the | ||
* preloaded state from the server. | ||
*/ | ||
module.exports = (function() { | ||
var store; | ||
|
||
function initialize(preloadedState) { | ||
store = redux.createStore(counter, preloadedState); | ||
return store; | ||
} | ||
|
||
function getStore() { | ||
return store; | ||
} | ||
|
||
return { | ||
initialize: initialize, | ||
getStore: getStore, | ||
}; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters