Skip to content

Commit

Permalink
Move prepare method to alt
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 25, 2015
1 parent 375ec30 commit c56d0bf
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
22 changes: 22 additions & 0 deletions docs/prepare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
layout: docs
title: Prepare
description: Prepare a payload for bootstrapping
permalink: /docs/prepare/
---

# prepare

> (Store: AltStore, payload: mixed): string
Given a store and a payload this functions returns a serialized string you can use to bootstrap that particular store.

```js
const data = alt.prepare(TodoStore, {
todos: [{
text: 'Buy some milk'
]}
});

alt.bootstrap(data);
```
6 changes: 0 additions & 6 deletions src/alt/AltStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ export default class AltStore {
this[EE].removeListener('change', cb)
}

prepare(state) {
const data = {}
data[this._storeName] = state
return this[ALT].serialize(data)
}

getState() {
return this[ALT].getState(this[STATE_CONTAINER])
}
Expand Down
9 changes: 9 additions & 0 deletions src/alt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ class Alt {
})
}

prepare(store, payload) {
const data = {}
if (!store._storeName) {
throw new ReferenceError('Store provided does not have a name')
}
data[store._storeName] = payload
return this.serialize(data)
}

// Instance type methods for injecting alt into your application as context

addActions(name, ActionsClass, ...args) {
Expand Down
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,21 @@ const tests = {

actions.fire()
},

'prepare a payload for bootstrap'() {
const serialized = alt.prepare(myStore, { a: 1, b: 2 })
const parsed = JSON.parse(serialized)

assert.isString(serialized)
assert.isObject(parsed.MyStore)
assert(parsed.MyStore.a === 1)
assert(parsed.MyStore.b === 2)
assert.isUndefined(parsed.MyStore.c)

assert.throws(() => {
alt.prepare({}, { x: 0 })
}, ReferenceError)
},
}

export default tests

0 comments on commit c56d0bf

Please sign in to comment.