A browser hash listener system using query string style parameters ( #foo=bar&baz=qux
), based on the subscribe design pattern.
Features:
- Browser Hash initialization
- Subscribe to Hash specific parameters
- Set specific Hash parameter
- Mute / Unmute subscription
$ npm install hash.subscribe
var Hash = require('hash.subscribe');
// Init browser hash
Hash.init('foo=bar');
// Set parameters (object)
Hash.setHash({
foo: ['bar'],
baz: ['qux']
});
// Set hash (string)
Hash.setHash('foo=bar&baz=qux');
// Update one key value
Hash.updateHashKeyValue('foo', ['bar1', 'bar2']);
// Delete one param
Hash.deleteParam('baz');
// Subscribe to parameter(s)
Hash.subscribe(['foo', 'baz'], function(params) {
if (params.foo.changed) {
console.log('Param Foo has changed : ' + params.foo.values);
}
});
// More examples in the test file...
Parameter:
hash
- String | Object - default hash to initialize the page with
Get current hash
Parameter:
keepHash
- Boolean - whether or not to keep the hash character in the return string
Returns: String
Get current hash parameters
Returns: Object
Get values of one hash parameter
Parameter:
key
- string - parameter name
Returns: Array
Updates current hash
Parameter:
hash
- String | Object - new hash
Updates one hash key
Parameters:
key
- String - hash key to updatevalue
- Array - new value(s) for key
Deletes hash param(s)
Parameter:
params
- String | Array - param(s) name(s) to delete
Subscribe to specific parameters
Parameters:
hashParameters
- Array - Array of parameters names you want to subscribe tocallback
- Function - Callback function called when a parameter has changed
Mutes subscription to Hash
Resume subscription to Hash
Destroys current hash value
$ git clone [email protected]:raiseandfall/Hash.subscribe.git && cd Hash.subscribe
$ npm run dev
$ npm run test
MIT