hashstate
v0.1.2
Published
Location hash manager for the web inspired by YUI3
Downloads
1
Maintainers
Readme
hashstate
Location hash manager for the web inspired by YUI3.
Installation
cortex install hashstate --save
Usage
var hashState = require('hashstate')(options);
- options
Object=
- prefix
String='!'
Prefix to prepend when setting the hash fragment. Default to'!'
- split
String=','
String to split the key-value pairs. - assign
String='='
String to join key and value of a key-value pair. - window
Window=
- prefix
The instance of hashState
is an EventEmitter.
.stringify(object)
Returns String
, excluding options.prefix
.
hashState.stringify({a: 1, b: 'c'}); // 'a=1,b=c'
.parse(string)
Parses the given string
and returns Object
the parsed object according to options.split
and options.assign
.
// Will deal with the leading '#!'
hashState.parse('#!a=1,b=c'); // {a: '1', b: 'c'}
hashState.parse('!a=1,b=c'); // {a: '1', b: 'c'}
hashState.parse('#a=1,b=c'); // {a: '1', b: 'c'}
hashState.parse('a=1,b=c'); // {a: '1', b: 'c'}
.setHash(hash, options)
- hash
String
hash string. - options
Object
- mute
Boolean
iftrue
, calling this method will not emit a'hashchange'
event.
- mute
Changes the current location hash, which will add a new history entry.
.replaceHash(hash, options)
- hash
String
hash string. - options
Object
- mute
Boolean
iftrue
, calling this method will not emit a'hashchange'
event.
- mute
Changes location hash and replaces the current history entry instead of adding a new entry.
.getHash()
Returns the current hash string, excluding the leading '#'
and options.prefix
.
Event: hashchange
- e
Object
- newHash
String
the new(current) hash string. - oldHash
String
the old hash string.
- newHash
Emitted when hash changes.
hashState.on('hashchange', function(e){
console.log('hashchange:', e.newHash);
});