Comparing version 0.2.3 to 0.2.4
{ | ||
"name": "reflux", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"homepage": "https://github.com/spoike/reflux", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -5,2 +5,8 @@ # Changelog | ||
## v0.2.4 | ||
* Promisable actions [#186](https://github.com/spoike/refluxjs/issues/186) | ||
* Fixed IE8 bug [#202](https://github.com/spoike/refluxjs/issues/202), [#187](https://github.com/spoike/refluxjs/issues/187) | ||
* Plus other various fixes: [#201](https://github.com/spoike/refluxjs/issues/201), [#200](https://github.com/spoike/refluxjs/issues/202), [#183](https://github.com/spoike/refluxjs/issues/183), and [#182](https://github.com/spoike/refluxjs/issues/182) | ||
## v0.2.3 | ||
@@ -7,0 +13,0 @@ |
@@ -8,3 +8,3 @@ module.exports = function(grunt) { | ||
options: { | ||
jshintrc: '.jshintrc' | ||
jshintrc: true | ||
} | ||
@@ -11,0 +11,0 @@ }, |
{ | ||
"name": "reflux", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "A simple library for uni-directional dataflow application architecture inspired by ReactJS Flux", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"test": "grunt test", | ||
"benchmark": "node test/benchmarks" | ||
"benchmark": "node test/benchmarks", | ||
"build": "grunt build" | ||
}, | ||
@@ -17,4 +18,9 @@ "author": "Mikael Brassman", | ||
}, | ||
"keywords": [ | ||
"react", | ||
"flux" | ||
], | ||
"dependencies": { | ||
"eventemitter3": "^0.1.2" | ||
"eventemitter3": "^0.1.2", | ||
"native-promise-only": "^0.7.6-a" | ||
}, | ||
@@ -21,0 +27,0 @@ "devDependencies": { |
@@ -169,2 +169,40 @@ # RefluxJS | ||
##### Asynchronous actions as Promises | ||
Asynchronous actions can used as promises, which is particularly useful for server-side rendering when you must await the successful (or failed) completion of an action before rendering. | ||
Suppose you had an action + store to make an API request: | ||
```javascript | ||
// Create async action with `completed` & `failed` children | ||
var makeRequest = Reflux.createAction({ asyncResult: true }); | ||
var RequestStore = Reflux.createStore({ | ||
init: function() { | ||
this.listenTo(makeRequest, 'onMakeRequest'); | ||
}, | ||
onMakeRequest: function(url) { | ||
// Assume `request` is some HTTP library (e.g. superagent) | ||
request(url, function(response) { | ||
if (response.ok) { | ||
makeRequest.completed(response.body); | ||
} else { | ||
makeRequest.failed(response.error); | ||
} | ||
} | ||
} | ||
}); | ||
``` | ||
Then, on the server, you could use promises to make the request and either render or serve an error: | ||
```javascript | ||
makeRequest('/api/somethign').then(function(body) { | ||
// Render the response body | ||
}).catch(function(err) { | ||
// Handle the API error object | ||
}); | ||
``` | ||
#### Action hooks | ||
@@ -268,3 +306,3 @@ | ||
Methods from mixins is available as well as the methods declared in the Store. So it's possible to access store's `this` from mixin, or methods of mixin from methods of store: | ||
Methods from mixins are available as well as the methods declared in the Store. So it's possible to access store's `this` from mixin, or methods of mixin from methods of store: | ||
@@ -505,2 +543,26 @@ ```javascript | ||
### Switching Promise library | ||
Don't like to use the Promise library provided? You can switch to another one, such as [Bluebird](https://github.com/petkaantonov/bluebird/) like this: | ||
```javascript | ||
// Do this before triggering actions | ||
Reflux.setPromise(require('bluebird')); | ||
``` | ||
*Note that promises are constructed with `new Promise(...)`. If your Promise library uses factories (e.g. `Q`), then use `Reflux.setPromiseFactory` instead.* | ||
### Switching Promise factory | ||
Since most Promise libraries use constructors (e.g. `new Promise(...)`), this is the default behavior. | ||
However, if you use `Q` or another library that uses a factory method, you can use `Reflux.setPromiseFactory` for it. | ||
```javascript | ||
// Do this before triggering actions | ||
Reflux.setPromiseFactory(require('Q').Promise); | ||
``` | ||
### Switching nextTick | ||
@@ -507,0 +569,0 @@ |
@@ -17,3 +17,3 @@ var _ = require('./utils'), | ||
if (!_.isObject(definition)){ | ||
definition = {name: definition}; | ||
definition = {actionName: definition}; | ||
} | ||
@@ -20,0 +20,0 @@ |
@@ -59,3 +59,22 @@ exports.ActionMethods = require('./ActionMethods'); | ||
/** | ||
* Sets the Promise library that Reflux uses | ||
*/ | ||
exports.setPromise = function(ctx) { | ||
var _ = require('./utils'); | ||
_.Promise = ctx; | ||
}; | ||
/** | ||
* Sets the Promise factory that creates new promises | ||
* @param {Function} factory has the signature `function(resolver) { return [new Promise]; }` | ||
*/ | ||
exports.setPromiseFactory = function(factory) { | ||
var _ = require('./utils'); | ||
_.createPromise = factory; | ||
}; | ||
/** | ||
* Sets the method used for deferring actions and stores | ||
@@ -62,0 +81,0 @@ */ |
@@ -66,3 +66,3 @@ /** | ||
return function() { | ||
var i, subs = context.subscriptions; | ||
var i, subs = context.subscriptions, | ||
index = (subs ? subs.indexOf(subobj) : -1); | ||
@@ -69,0 +69,0 @@ _.throwIf(index === -1,'Tried to remove join already gone from subscriptions list!'); |
@@ -10,4 +10,4 @@ var _ = require('./utils'), | ||
*/ | ||
mapChildListenables = function(listenable) { | ||
var i = 0, children = {}; | ||
var mapChildListenables = function(listenable) { | ||
var i = 0, children = {}, childName; | ||
for (;i < (listenable.children||[]).length; ++i) { | ||
@@ -28,3 +28,3 @@ childName = listenable.children[i]; | ||
*/ | ||
flattenListenables = function(listenables) { | ||
var flattenListenables = function(listenables) { | ||
var flattened = {}; | ||
@@ -176,3 +176,3 @@ for(var key in listenables){ | ||
if (_.isFunction(defaultCallback) && _.isFunction(listenable.getInitialState)) { | ||
data = listenable.getInitialState(); | ||
var data = listenable.getInitialState(); | ||
if (data && _.isFunction(data.then)) { | ||
@@ -179,0 +179,0 @@ data.then(function() { |
@@ -64,3 +64,5 @@ var _ = require('./utils'); | ||
return me.completed(response); | ||
}).catch(function(error) { | ||
}); | ||
// IE compatibility - catch is a reserved word - without bracket notation source compilation will fail under IE | ||
promise["catch"](function(error) { | ||
return me.failed(error); | ||
@@ -81,2 +83,7 @@ }); | ||
return this.listen(function() { | ||
if (!callback) { | ||
throw new Error('Expected a function returning a promise but got ' + callback); | ||
} | ||
var args = arguments, | ||
@@ -108,3 +115,37 @@ promise = callback.apply(bindContext, args); | ||
}); | ||
} | ||
}, | ||
/** | ||
* Returns a Promise for the triggered action | ||
*/ | ||
triggerPromise: function(){ | ||
var me = this; | ||
var args = arguments; | ||
var canHandlePromise = | ||
this.children.indexOf('completed') >= 0 && | ||
this.children.indexOf('failed') >= 0; | ||
if (!canHandlePromise){ | ||
throw new Error('Publisher must have "completed" and "failed" child publishers'); | ||
} | ||
var promise = _.createPromise(function(resolve, reject) { | ||
var removeSuccess = me.completed.listen(function(args) { | ||
removeSuccess(); | ||
removeFailed(); | ||
resolve(args); | ||
}); | ||
var removeFailed = me.failed.listen(function(args) { | ||
removeSuccess(); | ||
removeFailed(); | ||
reject(args); | ||
}); | ||
me.triggerAsync.apply(me, args); | ||
}); | ||
return promise; | ||
}, | ||
}; |
@@ -50,5 +50,10 @@ /* | ||
exports.Promise = require("native-promise-only"); | ||
exports.createPromise = function(resolver) { | ||
return new exports.Promise(resolver); | ||
}; | ||
exports.isArguments = function(value) { | ||
return value && typeof value == 'object' && typeof value.length == 'number' && | ||
(toString.call(value) === '[object Arguments]' || (hasOwnProperty.call(value, 'callee' && !propertyIsEnumerable.call(value, 'callee')))) || false; | ||
return typeof value === 'object' && ('callee' in value) && typeof value.length === 'number'; | ||
}; | ||
@@ -55,0 +60,0 @@ |
Sorry, the diff of this file is not supported yet
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
67784
947
669
2
+ Addednative-promise-only@^0.7.6-a
+ Addednative-promise-only@0.7.6-a(transitive)