new State( template )

This class manages all the getting and setting of its' parent templates' state.

Parameters
Name Type Description
template FooGallery.Template

The template to manage the state for.

Members


<readonly> apiEnabled :boolean

Whether or not the history API is enabled in the current browser.

Details
boolean

opt :FooGallery.State~Options

The state specific options for the template.


enabled :boolean

Whether or not the component is enabled.

Details
boolean

pushOrReplace :string

Which method of the history API to use by default when updating the state.

Details
string

"replace"


<readonly> regex :Object

An object containing regular expressions used to test and parse a hash value into a state object.

Description

The regular expressions contained within this object are specific to this template and are created using the template id and the delimiters from the options.

Details
Object

tmpl :FooGallery.Template

The template that created this component.

Methods


destroy()

Destroy the component clearing any current state from the url and preparing it for garbage collection.


isPushOrReplace( value ) → {boolean}

Check if the supplied value is "push" or "replace".

Parameters
Name Type Description
value *

The value to check.

Returns

exists() → {boolean}

Check if the current url contains state for this template.

Returns

parse() → {object}

Parse the current url returning an object containing all values for the template.

Description

This method always returns an object, if successful the object contains properties otherwise it is just a plain empty object. For this method to be successful the current template id must match the one from the url.

Returns

hashify( state ) → {string}

Converts the supplied state object into a string value to use as a hash.

Parameters
Name Type Description
state object

The object to hashify.

Returns

replace( state )

Replace the current state with the one supplied.

Parameters
Name Type Description
state object

The state to replace the current with.


push( state )

Push the supplied state into the browser history.

Parameters
Name Type Description
state object

The state to push.


update( state [, pushOrReplace ] )

Update the browser history using the supplied state.

Parameters
Name Type Attributes Description
state object

The state to update.

pushOrReplace string <optional>

The method to use to update the state. If not supplied this falls back to the value of the pushOrReplace property.


clear()

Clear the template state from the current browser history if it exists.


initial() → {FooGallery~State}

Get the initial start up state of the template.

Description

This method returns an initial start up state from the template options.


get( [ item ] ) → {FooGallery~State}

Get the current state of the template.

Description

This method does not parse the history or url it returns the current state of the template itself. To parse the current url use the parse method instead.

Parameters
Name Type Attributes Description
item FooGallery.Item <optional>

If supplied the items' id is included in the resulting state object.


<static> extend( [ definition ] ) → {function}

Creates a new class that inherits from this one which in turn allows itself to be extended.

Description

Every class created using this method has both the extend and override static methods added to it to allow it to be extended.

Parameters
Name Type Attributes Description
definition Object <optional>

An object containing any methods to implement/override.

Returns

A new class that inherits from the base class.

Examples

The below shows an example of how to implement inheritance using this method.


// create a base Person class
var Person = FooGallery.utils.Class.extend({
	construct: function(isDancing){
		this.dancing = isDancing;
	},
	dance: function(){
		return this.dancing;
	}
});

var Ninja = Person.extend({
	construct: function(){
		// Call the inherited version of construct()
		this._super( false );
	},
	dance: function(){
		// Call the inherited version of dance()
		return this._super();
	},
	swingSword: function(){
		return true;
	}
});

var p = new Person(true);
console.log( p.dance() ); // => true

var n = new Ninja();
console.log( n.dance() ); // => false
console.log( n.swingSword() ); // => true
console.log(
	p instanceof Person && p.constructor === Person && p instanceof FooGallery.utils.Class
	&& n instanceof Ninja && n.constructor === Ninja && n instanceof Person && n instanceof FooGallery.utils.Class
); // => true


<static> override( name, fn )

Overrides a single method on this class.

Description

This is a helper method for overriding a single function of a FooGallery.utils.Class or one of its child classes. This uses the FooGallery.utils.fn.addOrOverride method internally and simply provides the correct prototype.

Parameters
Name Type Description
name string

The name of the function to override.

fn function

The new function to override with, the _super method will be made available within this function.

Examples

The below example wraps the Person.prototype.dance method with a new one that inverts the result. Note the override applies even to instances of the class that are already created.


var Person = FooGallery.utils.Class.extend({
  construct: function(isDancing){
    this.dancing = isDancing;
  },
  dance: function(){
    return this.dancing;
  }
});

var p = new Person(true);
console.log( p.dance() ); // => true

Person.override("dance", function(){
	// Call the original version of dance()
	return !this._super();
});

console.log( p.dance() ); // => false


set( state )

Set the current state of the template.

Description

This method does not set the history or url it sets the current state of the template itself. To update the url use the update method instead.

Parameters
Name Type Description
state FooGallery~State

The state to set the template to.

Type Definitions


Options

An object containing the state options for the template.

Properties
Name Type Attributes Default Description
enabled boolean <optional>
false

Whether or not state is enabled for the template.

pushOrReplace string <optional>
"replace"

Which method of the history API to use by default when updating the state.

values string <optional>
"/"

The delimiter used between key value pairs in the hash.

pair string <optional>
":"

The delimiter used between a key and a value in the hash.

array string <optional>
"+"

The delimiter used for array values in the hash.

Details
object