abstract-state-router
Advanced tools
Comparing version 3.1.0 to 3.2.0
34
index.js
@@ -123,3 +123,3 @@ var StateState = require('./state-state') | ||
return prototypalStateHolder.guaranteeAllStatesExist(newStateName) | ||
return promiseMe(prototypalStateHolder.guaranteeAllStatesExist, newStateName) | ||
.then(function applyDefaultParameters() { | ||
@@ -206,7 +206,6 @@ var state = prototypalStateHolder.get(newStateName) | ||
function getDestinationUrl(stateName, parameters) { | ||
return prototypalStateHolder.guaranteeAllStatesExist(stateName).then(function() { | ||
var route = prototypalStateHolder.buildFullStateRoute(stateName) | ||
return buildPath(route, parameters || {}) | ||
}) | ||
function makePath(stateName, parameters) { | ||
prototypalStateHolder.guaranteeAllStatesExist(stateName) | ||
var route = prototypalStateHolder.buildFullStateRoute(stateName) | ||
return buildPath(route, parameters || {}) | ||
} | ||
@@ -223,6 +222,6 @@ | ||
return getDestinationUrl(newStateName, parameters).then(goFunction, handleError.bind(null, 'stateChangeError')) | ||
return promiseMe(makePath, newStateName, parameters).then(goFunction, handleError.bind(null, 'stateChangeError')) | ||
} | ||
stateProviderEmitter.evaluateCurrentRoute = function evaluateCurrentRoute(defaultRoute, defaultParams) { | ||
return getDestinationUrl(defaultRoute, defaultParams).then(function(defaultPath) { | ||
return promiseMe(makePath, defaultRoute, defaultParams).then(function(defaultPath) { | ||
hashRouter.evaluateCurrent(defaultPath) | ||
@@ -233,3 +232,14 @@ }).catch(function(err) { | ||
} | ||
stateProviderEmitter.makePath = function makePathAndPrependHash(stateName, parameters) { | ||
try { | ||
return '#' + makePath(stateName, parameters) | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
if (renderer.setUpMakePathFunction) { | ||
renderer.setUpMakePathFunction(stateProviderEmitter.makePath) | ||
} | ||
return stateProviderEmitter | ||
@@ -304,1 +314,9 @@ } | ||
} | ||
function promiseMe() { | ||
var fn = Array.prototype.shift.apply(arguments) | ||
var args = arguments | ||
return new Promise(function(resolve) { | ||
resolve(fn.apply(null, args)) | ||
}) | ||
} |
{ | ||
"name": "abstract-state-router", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "The basics of a client-side state router ala the AngularJS ui-router, but without any DOM interactions", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -29,3 +29,3 @@ To manage webapp states so that you don't have to deal with url paths or anything. | ||
`data` is an object that can hold whatever you want - it will be passed in to the resolve and callback functions. | ||
`data` is an object that can hold whatever you want - it will be passed in to the resolve and activate functions. | ||
@@ -57,6 +57,51 @@ `template` is a template string/object/whatever to be interpreted by the render function. | ||
###events | ||
### events | ||
- 'destroy': emitted when the state is destroyed | ||
```js | ||
stateRouter.addState({ | ||
name: 'app', | ||
data: {}, | ||
route: '/app', | ||
template: '', | ||
defaultChild: 'tab1', | ||
resolve: function(data, parameters, cb) { | ||
// Sync or asnyc stuff; just call the callback when you're done | ||
isLoggedIn(function(err, isLoggedIn) { | ||
cb(err, isLoggedIn) | ||
}) | ||
}, activate: function(context) { | ||
// Normally, you would set data in your favorite view library | ||
var isLoggedIn = context.content | ||
var ele = document.getElementById('status') | ||
ele.innerText = isLoggedIn ? 'Logged In!' : 'Logged Out!' | ||
} | ||
}) | ||
stateRouter.addState({ | ||
name: 'app.tab1', | ||
data: {}, | ||
route: '/tab_1', | ||
template: '', | ||
resolve: function(data, parameters, cb) { | ||
getTab1Data(cb) | ||
}, activate: function(context) { | ||
document.getElementById('tab').innerText = context.content | ||
} | ||
}) | ||
stateRouter.addState({ | ||
name: 'app.tab2', | ||
data: {}, | ||
route: '/tab_2', | ||
template: '', | ||
resolve: function(data, parameters, cb) { | ||
getTab2Data(cb) | ||
}, activate: function(context) { | ||
document.getElementById('tab').innerText = context.content | ||
} | ||
}) | ||
``` | ||
# stateRouter.go(stateName, [parameters, [options]]) | ||
@@ -68,8 +113,17 @@ | ||
If a state change is triggered during a state transition, it is queued and applied once the current state change is done. | ||
If a state change is triggered during a state transition, and the DOM hasn't been manipulated yet, then the current state change is discarded, and the new one replaces it. Otherwise, it is queued and applied once the current state change is done. | ||
# stateRouter.evaluateCurrentRoute(defaultRoute, defaultParameters) | ||
```js | ||
stateRouter.go('app') | ||
// This actually redirects to app.tab1, because the app state has the default child: 'tab1' | ||
``` | ||
You'll want to call this once you've added all your initial states. It causes the current path to be evaluated, and will activate the current state. If the current path doesn't match the route of any available states, the browser gets sent to the default route provided. | ||
# stateRouter.evaluateCurrentRoute(fallbackRoute, [fallbackParameters]) | ||
You'll want to call this once you've added all your initial states. It causes the current path to be evaluated, and will activate the current state. If the current path doesn't match the route of any available states, the browser gets sent to the fallback route provided. | ||
```js | ||
stateRouter.evaluateCurrentRoute('app.tab2') | ||
``` | ||
# State change flow | ||
@@ -83,3 +137,3 @@ | ||
- destroy appropriate dom elements | ||
- reset "change"ing dom elements - still needs implemented | ||
- reset "change"ing dom elements | ||
- call render functions for "create"ed states | ||
@@ -86,0 +140,0 @@ - call all activate functions |
var stateStringParser = require('./state-string-parser') | ||
var Promise = require('promise') | ||
var parse = require('./state-string-parser') | ||
@@ -38,14 +37,10 @@ | ||
function guaranteeAllStatesExist(newStateName) { | ||
return new Promise(function(resolve) { | ||
var stateNames = parse(newStateName) | ||
var statesThatDontExist = stateNames.filter(function(name) { | ||
return !states[name] | ||
}) | ||
var stateNames = parse(newStateName) | ||
var statesThatDontExist = stateNames.filter(function(name) { | ||
return !states[name] | ||
}) | ||
if (statesThatDontExist.length > 0) { | ||
throw new Error('State ' + statesThatDontExist[statesThatDontExist.length - 1] + ' does not exist') | ||
} | ||
resolve() | ||
}) | ||
if (statesThatDontExist.length > 0) { | ||
throw new Error('State ' + statesThatDontExist[statesThatDontExist.length - 1] + ' does not exist') | ||
} | ||
} | ||
@@ -52,0 +47,0 @@ |
@@ -39,3 +39,4 @@ var myArbitraryRenderFunction = function lol(parent, cb) { | ||
}, 100) | ||
} | ||
}, | ||
setUpMakePathFunction: function noop() {} | ||
} |
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
71070
2073
155