Comparing version 0.1.0 to 0.2.0
22
index.js
@@ -26,3 +26,3 @@ (function() { | ||
store[action].apply(store, actionArgs); | ||
} else {} | ||
} | ||
}); | ||
@@ -142,7 +142,17 @@ }, | ||
DocBrown.storeMixin = function(store) { | ||
if (!store) { | ||
throw new Error("Missing store"); | ||
var _getStore; | ||
if (typeof store === "function") { | ||
_getStore = store; | ||
} else if (typeof store === "object") { | ||
_getStore = function() { | ||
return store; | ||
}; | ||
} else { | ||
throw new Error("Unsupported store retriever."); | ||
} | ||
return { | ||
getStore: function() { | ||
var store = _getStore(); | ||
if (!store) | ||
throw new Error("Missing store"); | ||
return store; | ||
@@ -155,9 +165,9 @@ }, | ||
}.bind(this); | ||
return store.getState(); | ||
return this.getStore().getState(); | ||
}, | ||
componentDidMount: function() { | ||
store.subscribe(this.__changeListener); | ||
this.getStore().subscribe(this.__changeListener); | ||
}, | ||
componentWillUnmount: function() { | ||
store.unsubscribe(this.__changeListener); | ||
this.getStore().unsubscribe(this.__changeListener); | ||
delete this.__changeListener; | ||
@@ -164,0 +174,0 @@ } |
{ | ||
"name": "docbrown", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Flux experiment.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" | ||
"test": "istanbul cover node_modules/.bin/_mocha --report html --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" | ||
}, | ||
@@ -9,0 +9,0 @@ "devDependencies": { |
@@ -171,2 +171,30 @@ DocBrown | ||
Dynamic store retriever | ||
----------------------- | ||
When applying the `storeMixin` at react class declaration time, it might happen that your store instance isn't created just yet; in that case you can pass a function to the `storeMixin` function instead of a store object: | ||
```js | ||
// registry module | ||
module.exports = {}; | ||
// app module | ||
var registry = require("registry") | ||
// … | ||
registry.timeStore = new TimeStore(); | ||
// … | ||
// view module | ||
var registry = require("registry"); | ||
var Counter = React.createClass({ | ||
mixins: [DocBrown.storeMixin(function() { | ||
return registry.timeStore; | ||
})], | ||
actions: [Actions], | ||
// … | ||
}); | ||
``` | ||
That way, the mixin will only try to retrieve the store instance at component mount time. | ||
Install | ||
@@ -173,0 +201,0 @@ ======= |
26
test.js
@@ -407,8 +407,26 @@ var DocBrown = require("./"); | ||
describe("DocBrown.storeMixin()", function() { | ||
it("should require a store", function() { | ||
it("should require a store retriever", function() { | ||
expect(function() { | ||
DocBrown.storeMixin(); | ||
}).to.Throw("Missing store"); | ||
}).to.Throw("Unsupported store retriever."); | ||
}); | ||
it("should accept a store instance as a retriever", function() { | ||
var fakeStore = {fakeStore: true}; | ||
var mixin = DocBrown.storeMixin(fakeStore); | ||
expect(mixin.getStore()).eql(fakeStore); | ||
}); | ||
it("should accept a function as a retriever", function() { | ||
var fakeStore = {fakeStore: true}; | ||
var mixin = DocBrown.storeMixin(function() { | ||
return fakeStore; | ||
}); | ||
expect(mixin.getStore()).eql(fakeStore); | ||
}); | ||
describe("constructed", function() { | ||
@@ -422,3 +440,5 @@ var dispatcher, Actions, store, mixin; | ||
store = new Store(); | ||
mixin = DocBrown.storeMixin(store); | ||
mixin = DocBrown.storeMixin(function() { | ||
return store; | ||
}); | ||
}); | ||
@@ -425,0 +445,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28925
578
216