Skip to content

Commit

Permalink
Make stores available in alt.stores
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Jan 22, 2015
1 parent f4179e6 commit 598624c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
23 changes: 11 additions & 12 deletions dist/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var LISTENERS = Symbol("stores action listeners storage");
var STATE_CONTAINER = VariableSymbol("the state container");
var STORE_BOOTSTRAP = Symbol("event handler onBootstrap");
var STORE_SNAPSHOT = Symbol("event handler onTakeSnapshot");
var STORES_STORE = Symbol("stores storage");

var formatAsConstant = function (name) {
return name.replace(/[a-z]([A-Z])/g, function (i) {
Expand Down Expand Up @@ -169,27 +168,27 @@ var StoreMixin = {
var bootstrap = function (instance, data) {
var obj = JSON.parse(data);
Object.keys(obj).forEach(function (key) {
Object.assign(instance[STORES_STORE][key][STATE_CONTAINER], obj[key]);
if (instance[STORES_STORE][key][STORE_BOOTSTRAP]) {
instance[STORES_STORE][key][STORE_BOOTSTRAP]();
Object.assign(instance.stores[key][STATE_CONTAINER], obj[key]);
if (instance.stores[key][STORE_BOOTSTRAP]) {
instance.stores[key][STORE_BOOTSTRAP]();
}
});
};

var snapshot = function (instance) {
return JSON.stringify(Object.keys(instance[STORES_STORE]).reduce(function (obj, key) {
if (instance[STORES_STORE][key][STORE_SNAPSHOT]) {
instance[STORES_STORE][key][STORE_SNAPSHOT]();
return JSON.stringify(Object.keys(instance.stores).reduce(function (obj, key) {
if (instance.stores[key][STORE_SNAPSHOT]) {
instance.stores[key][STORE_SNAPSHOT]();
}
obj[key] = instance[STORES_STORE][key].getState();
obj[key] = instance.stores[key].getState();
return obj;
}, {}));
};

var Alt = (function () {
var Alt = function Alt() {
this.dispatcher = new Dispatcher();
this[STORES_STORE] = {};
this.stores = {};
};

Alt.prototype.createStore = function (StoreModel, iden) {
Expand All @@ -207,17 +206,17 @@ var Alt = (function () {
_storeName: key,
dispatcher: this.dispatcher,
getInstance: function () {
return _this3[STORES_STORE][key];
return _this3.stores[key];
}
});

var store = new Store();

if (this[STORES_STORE][key]) {
if (this.stores[key]) {
throw new ReferenceError("A store named " + key + " already exists, double check your store names or pass in\nyour own custom identifier for each store");
}

return this[STORES_STORE][key] = Object.assign(new AltStore(this.dispatcher, store), getInternalMethods(StoreModel, builtIns));
return this.stores[key] = Object.assign(new AltStore(this.dispatcher, store), getInternalMethods(StoreModel, builtIns));
};

Alt.prototype.createActions = function (ActionsClass) {
Expand Down
23 changes: 11 additions & 12 deletions src/alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var LISTENERS = Symbol('stores action listeners storage')
var STATE_CONTAINER = VariableSymbol('the state container')
var STORE_BOOTSTRAP = Symbol('event handler onBootstrap')
var STORE_SNAPSHOT = Symbol('event handler onTakeSnapshot')
var STORES_STORE = Symbol('stores storage')

var formatAsConstant = (name) => {
return name.replace(/[a-z]([A-Z])/g, (i) => {
Expand Down Expand Up @@ -164,20 +163,20 @@ var StoreMixin = {
var bootstrap = (instance, data) => {
var obj = JSON.parse(data)
Object.keys(obj).forEach((key) => {
Object.assign(instance[STORES_STORE][key][STATE_CONTAINER], obj[key])
if (instance[STORES_STORE][key][STORE_BOOTSTRAP]) {
instance[STORES_STORE][key][STORE_BOOTSTRAP]()
Object.assign(instance.stores[key][STATE_CONTAINER], obj[key])
if (instance.stores[key][STORE_BOOTSTRAP]) {
instance.stores[key][STORE_BOOTSTRAP]()
}
})
}

var snapshot = (instance) => {
return JSON.stringify(
Object.keys(instance[STORES_STORE]).reduce((obj, key) => {
if (instance[STORES_STORE][key][STORE_SNAPSHOT]) {
instance[STORES_STORE][key][STORE_SNAPSHOT]()
Object.keys(instance.stores).reduce((obj, key) => {
if (instance.stores[key][STORE_SNAPSHOT]) {
instance.stores[key][STORE_SNAPSHOT]()
}
obj[key] = instance[STORES_STORE][key].getState()
obj[key] = instance.stores[key].getState()
return obj
}, {})
)
Expand All @@ -186,7 +185,7 @@ var snapshot = (instance) => {
class Alt {
constructor() {
this.dispatcher = new Dispatcher()
this[STORES_STORE] = {}
this.stores = {}
}

createStore(StoreModel, iden) {
Expand All @@ -200,19 +199,19 @@ class Alt {
Object.assign(Store.prototype, StoreMixin, {
_storeName: key,
dispatcher: this.dispatcher,
getInstance: () => this[STORES_STORE][key]
getInstance: () => this.stores[key]
})

var store = new Store()

if (this[STORES_STORE][key]) {
if (this.stores[key]) {
throw new ReferenceError(
`A store named ${key} already exists, double check your store names or pass in
your own custom identifier for each store`
)
}

return this[STORES_STORE][key] = Object.assign(
return this.stores[key] = Object.assign(
new AltStore(this.dispatcher, store),
getInternalMethods(StoreModel, builtIns)
)
Expand Down
2 changes: 2 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ var lifecycleStore = alt.createStore(LifeCycleStore)
assert.equal(typeof myStore.removeListener, 'undefined', 'event emitter methods not present')
assert.equal(typeof myStore.emit, 'undefined', 'event emitter methods not present')

assert.equal(typeof alt.stores.AltSecondStore, 'object', 'store exists in alt.stores')

assert.equal(typeof myStore.externalMethod, 'function', 'static methods are made available')
assert.equal(myStore.externalMethod(), true, 'static methods return proper result')
assert.equal(typeof secondStore.externalMethod, 'function', 'static methods are made available')
Expand Down

0 comments on commit 598624c

Please sign in to comment.