Skip to content

Commit

Permalink
Fix clashing serialize/deserialize config methods
Browse files Browse the repository at this point in the history
- alt config.serialize clashes with store config.serialize (same with
  deserialize). Rename to store config to serializeData to remove
  conflict.
- Update docs too
  • Loading branch information
jdlehman committed Apr 27, 2015
1 parent 722465c commit b79425c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 38 deletions.
12 changes: 6 additions & 6 deletions docs/createStore.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ class TodoStore {
}
```

#### serialize
#### serializeData

`serialize` is also called before the store's state is serialized. You may optionally return an object, which will be used directly as the snapshot data for the store. If you do not return anything, the default, [`MyStore#getState()`](stores.md#storegetstate) is used for the snapshot data. See the [serialization](serialization.md) for an example.
`serializeData` is also called before the store's state is serialized. You may optionally return an object, which will be used directly as the snapshot data for the store. If you do not return anything, the default, [`MyStore#getState()`](stores.md#storegetstate) is used for the snapshot data. See the [serialization](serialization.md) for an example.

```js
class TodoStore {
static config = {
serialize: function(data) {
serializeData: function(data) {
// do something here
// optional return of data to be used in snapshot
// return mySnapshotData
Expand All @@ -62,14 +62,14 @@ class TodoStore {
}
```

#### deserialize
#### deserializeData

`deserialize` is called before the store's state is deserialized. This occurs whenever the store's state is being set to an existing snapshot/bootstrap data. Here you can perform any final tasks you need to before the snapshot/bootstrap data is set on the store such as mapping the data to model objects, or converting data an external source like a JSON API into a format the store expects. Deserialize takes in a parameter that is an object of snapshot/bootstrap data and must return the data to be set to the store's state. If nothing is returned, then the data from the snapshot is set to the store's state. See the [serialization](serialization.md) for an example.
`deserializeData` is called before the store's state is deserialized. This occurs whenever the store's state is being set to an existing snapshot/bootstrap data. Here you can perform any final tasks you need to before the snapshot/bootstrap data is set on the store such as mapping the data to model objects, or converting data an external source like a JSON API into a format the store expects. `deserializeData` takes in a parameter that is an object of snapshot/bootstrap data and must return the data to be set to the store's state. If nothing is returned, then the data from the snapshot is set to the store's state. See the [serialization](serialization.md) for an example.

```js
class TodoStore {
static config = {
deserialize: function(data) {
deserializeData: function(data) {
// do something here
return modifiedData
}
Expand Down
45 changes: 23 additions & 22 deletions docs/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ permalink: /docs/serialization/

# Serialization

The [serialize](lifecycleListeners.md#serialize) and [deserialize](lifecycleListeners.md#deserialize) lifecycle listener methods can be utilized separately or together to transform the store data being saved in a snapshot or the snapshot/bootstrap data being set to a store. Though they do not have to be used together, you can picture the parity between the two methods. You can transform the shape of your store data created in the snapshot with `serialize`, which might be to get your data in a format ready to be consumed by other services and use `deserialize` to transform this data back into the format that your store recognizes.
The [serializeData](createStore.md#serializeData) and [deserializeData](createStore.md#deserializeData) store config methods can be utilized separately or together to transform the store data being saved in a snapshot or the snapshot/bootstrap data being set to a store. Though they do not have to be used together, you can picture the parity between the two methods. You can transform the shape of your store data created in the snapshot with `serializeData`, which might be to get your data in a format ready to be consumed by other services and use `deserializeData` to transform this data back into the format that your store recognizes.

In the example below, we will show how this technique can be used to `serialize` complex model data being used by the store into a simpler structure for the store's snapshot, and repopulate our store models with the simple structure from the bootstrap/snapshot data with `deserialize`.
In the example below, we will show how this technique can be used to `serializeData` complex model data being used by the store into a simpler structure for the store's snapshot, and repopulate our store models with the simple structure from the bootstrap/snapshot data with `deserializeData`.

## Serialize
## serializeData

`serialize` provides a hook to transform the store data (via an optional return value) to be saved to an alt snapshot. If a return value is provided than it becomes the value of the store in the snapshot. For example, if the store name was `MyStore` and `serialize` returned `{firstName: 'Cereal', lastName: 'Eyes'}`, the snapshot would contain the data `{...'MyStore': {'firstName': 'Cereal', 'lastName': 'Eyes'}...}`. If there is no return value, the default, [`MyStore#getState()`](stores.md#storegetstate) is used for the snapshot data.
`serializeData` provides a hook to transform the store data (via an optional return value) to be saved to an alt snapshot. If a return value is provided than it becomes the value of the store in the snapshot. For example, if the store name was `MyStore` and `serializeData` returned `{firstName: 'Cereal', lastName: 'Eyes'}`, the snapshot would contain the data `{...'MyStore': {'firstName': 'Cereal', 'lastName': 'Eyes'}...}`. If there is no return value, the default, [`MyStore#getState()`](stores.md#storegetstate) is used for the snapshot data.

## Deserialize
## DeserializeData

`deserialize` provides a hook to transform snapshot/bootstrap data into a form acceptable to the store for use within the application. The return value of this function becomes the state of the store. If there is no return value, the state of the store from the snapshot is used verbatim. For example, if the `deserialize` received the data `{queryParams: 'val=2&val2=23'}`, we might transform the data into `{val: 2, val2: 23}` and return it to set the store data such that `myStore.val === 2` and `myStore.val2 === 23`. Deserialize can be useful for converting data from an external source such as a JSON API into the format the store expects.
`deserializeData` provides a hook to transform snapshot/bootstrap data into a form acceptable to the store for use within the application. The return value of this function becomes the state of the store. If there is no return value, the state of the store from the snapshot is used verbatim. For example, if the `deserializeData` received the data `{queryParams: 'val=2&val2=23'}`, we might transform the data into `{val: 2, val2: 23}` and return it to set the store data such that `myStore.val === 2` and `myStore.val2 === 23`. DeserializeData can be useful for converting data from an external source such as a JSON API into the format the store expects.

## Example

Expand Down Expand Up @@ -49,34 +49,35 @@ class MyModel {

// our store
class MyStore {
constructor() {

// our model is being used by the store
this.myModel = new Model({x: 2, y: 3})
// we want to change the property name of this value in the snapshot
this.anotherVal = 5
// we don't want to save this data in the snapshot
this.semiPrivateVal = 10

this.on('serialize', () => {
static config = {
serializeData: (state) => {
return {
// provides product and sum data from the model getters in addition to x and y
// this data would not be included by the default serialization (getState)
myModel: this.myModel.data,
myModel: state.myModel.data,
// change property name in snapshot
newValKeyName: this.anotherVal
newValKeyName: state.anotherVal
}
})

this.on('deserialize', (data) => {
},
deserializeData: (data) => {
const modifiedData = {
// need to take POJO and move data into the model our store expects
myModel: new MyModel({x: data.myModel.x, y: data.myModel.y}),
// change the property name back to what our store expects
anotherVal: data.newValKeyName
}
return modifiedData
})
}
}

constructor() {

// our model is being used by the store
this.myModel = new Model({x: 2, y: 3})
// we want to change the property name of this value in the snapshot
this.anotherVal = 5
// we don't want to save this data in the snapshot
this.semiPrivateVal = 10
}

static getMyModelData() {
Expand Down
8 changes: 4 additions & 4 deletions src/alt/utils/StateFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export function setAppState(instance, data, onStore) {
const store = instance.stores[key]
if (store) {
const { config } = store.StoreModel
if (config.deserialize) {
obj[key] = config.deserialize(obj[key]) || obj[key]
if (config.deserializeData) {
obj[key] = config.deserializeData(obj[key]) || obj[key]
}
assign(store[STATE_CONTAINER], obj[key])
onStore(store)
Expand All @@ -32,8 +32,8 @@ export function snapshot(instance, storeNames = []) {
if (store[LIFECYCLE].snapshot) {
store[LIFECYCLE].snapshot()
}
const customSnapshot = config.serialize &&
config.serialize(store[STATE_CONTAINER])
const customSnapshot = config.serializeData &&
config.serializeData(store[STATE_CONTAINER])
obj[storeName] = customSnapshot ? customSnapshot : store.getState()
return obj
}, {})
Expand Down
4 changes: 2 additions & 2 deletions src/utils/ImmutableUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ function immutable(store) {
return currentState
},

serialize: (state) => {
serializeData: (state) => {
return state.toJS()
},

deserialize: (data) => {
deserializeData: (data) => {
return Immutable.fromJS(data)
}
}
Expand Down
10 changes: 6 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,13 @@ const secondStore = alt.createStore(SecondStore, 'AltSecondStore')

class LifeCycleStore {
static config = {
serialize: (state) => {
serializeData: (state) => {
state.serialized = true
return state;
},
deserialize: (data) => {
deserializeData: (data) => {
data.deserialized = true
return data;
}
}

Expand Down Expand Up @@ -291,13 +293,13 @@ class Model {

class InterceptSnapshotStore {
static config = {
serialize: (state) => {
serializeData: (state) => {
return {
modelData: state.modelData.data,
anotherVal: state.anotherVal
}
},
deserialize: (data) => {
deserializeData: (data) => {
const obj = {
modelData: new Model({x: data.modelData.x, y: data.modelData.y}),
anotherVal: data.anotherVal
Expand Down

0 comments on commit b79425c

Please sign in to comment.