This shows you the differences between two versions of the page.
| documentation:api:libtf.persistence [2018/02/24 17:39] – created lee | documentation:api:libtf.persistence [2018/02/24 17:42] (current) – removed lee | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== LibTF.Persistence ====== | ||
| - | |||
| - | Most web browsers only allow for 10 megs (though some are as low as 5) of on-disk storage per origin. | ||
| - | |||
| - | In an attempt to minimize this issue, LibTF uses a defaults and delta persistence system. | ||
| - | |||
| - | In order to be persistent, all persistent objects must ultimately derive from the LibTF.PersistentObject class. | ||
| - | |||
| - | Each PersistentObject that is tracked by LibTF.Persistence has an " | ||
| - | |||
| - | A PersistentObject may be the " | ||
| - | |||
| - | You won't generally need to worry too much about the id hierarchy. | ||
| - | |||
| - | |||
| - | ===== LibTF.Persistence.setDefaults( id, objectClass, | ||
| - | |||
| - | * //id// is a toplevel persistence id which will be used to fetch the object later. | ||
| - | * // | ||
| - | * // | ||
| - | * Returns: Nothing. | ||
| - | * Throws: Error if: //id// contains invalid characters, // | ||
| - | |||
| - | setDefaults() must only be called at initialization time, from story javascript or initialization passages. | ||
| - | |||
| - | < | ||
| - | LibTF.Persistence.setDefaults( " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | " | ||
| - | ], | ||
| - | " | ||
| - | } ); | ||
| - | </ | ||
| - | |||
| - | |||
| - | ===== LibTF.Persistence.instantiate( id, forceClass=null ) ===== | ||
| - | |||
| - | * //id// is a persistence id denoting an object (whose class is ultimately derived from PersistentObject) to instantiate. | ||
| - | * // | ||
| - | * Returns: An instantiated object, previously defined with setDefaults(). | ||
| - | * Throws: Error if: //id// contains invalid characters, // | ||
| - | |||
| - | Instantiates a working copy of an object (whose class was ultimately derived from PersistentObject), | ||
| - | |||
| - | **DO NOT** store these objects themselves in the story format' | ||
| - | |||
| - | In the Sugarcube 2 and Snowman story formats, you may use the tfObj() function as a shorthand for this, since it is used so much. | ||
| - | |||
| - | < | ||
| - | var bob = LibTF.Persistence.instantiate( " | ||
| - | alert( " | ||
| - | </ | ||
| - | |||
| - | Sugarcube/ | ||
| - | < | ||
| - | alert( " | ||
| - | </ | ||
| - | |||
| - | |||
| - | ===== LibTF.Persistence.has( id ) ===== | ||
| - | |||
| - | * //id// is a persistence id to check for. | ||
| - | * Returns: True there is a data record for //id// in the LibTF persistence engine, or False if there is not. | ||
| - | * Throws: Error if: //id// contains invalid characters. | ||
| - | |||
| - | Checks to see if there is a record for //id// in the LibTF persistence engine. | ||
| - | |||
| - | < | ||
| - | if( LibTF.Persistence.has(" | ||
| - | alert( "It would appear that european_swallow has an irregular article defined!" | ||
| - | </ | ||
| - | |||
| - | |||
| - | ===== LibTF.Persistence.get( id ) ===== | ||
| - | |||
| - | * //id// is the persistence id whose data record we want to retrieve. | ||
| - | * Returns: The data record associated with //id//, or null if there isn't one. | ||
| - | * Throws: Error if: //id// contains invalid characters. | ||
| - | |||
| - | Checks to see if there is a record for //id// in the LibTF persistence engine. | ||
| - | |||
| - | < | ||
| - | var article = LibTF.Persistence.get( " | ||
| - | alert( "You see " + article | ||
| - | </ | ||
| - | (Would print "You see a european swallow here!", | ||
| - | |||
| - | |||
| - | ===== LibTF.Persistence.set( id, value ) ===== | ||
| - | |||
| - | * //id// is the persistence id whose data record we want to set. | ||
| - | * //value // is the value we want to set it to. | ||
| - | * Returns: Nothing. | ||
| - | * Throws: Error if: //id// contains invalid characters. | ||
| - | |||
| - | Sets the data record associated with //id// in the LibTF persistence engine. | ||
| - | |||
| - | < | ||
| - | LibTF.Persistence.set( " | ||
| - | </ | ||
| - | |||
| - | |||
| - | ===== LibTF.Persistence.reset( id ) ===== | ||
| - | |||
| - | * //id// is the persistence id whose data record we want to reset. | ||
| - | * Returns: the previous value, if the record was reset, or null if it was not reset. | ||
| - | * Throws: Error if: //id// contains invalid characters. | ||
| - | |||
| - | Resets the data record associated with //id// to whatever it was defined with in setDefaults(). | ||
| - | |||
| - | < | ||
| - | LibTF.Persistence.reset( " | ||
| - | </ | ||