Dependencies
None but the great jQuery

Typical usage
I believe this code is easy enough for a 5 minute read and use, but code samples may speed up the process quite a bit I guess


*Constructor and adding objects
// The constructor of ObjectStore expects a string which specify the name of the key property which is then used to identify the object.
//By default, ObjectStore uses "id"
var objStore = new ObjectStore("keyProp");    
//Add an object into the store. If an object with the same key already exists, an update will be performed instead
objStore.add(
    {
        keyProp: "obj1",   // The property to be used to identity the object in the hash table
        otherProp1: "Other thing",
        otherProp2: "Another thing"
    }
);

//Add a collection of objects into the store. It could be an array or a hash table of objects. 
//Actually, anything can be used with jquery.each can be added. But you need to make sure
//that everything that you added is a valid object for the object store being mainpulated
objStore.addCollection(collection);

objStore.addObjectStore(anotherObjectStoreWithSameKeyProperty);


*Update an object by its key property which is specified in the constructor
//If not found in the hash table, nothing would be done.
objStore.update(obj);


*Remove an object
objStore.remove(obj);   // Find the object in the store and remove it

objStore.remove(key);  // Find the object in the store by its key and remove it

objStore.clear();    // Clear the object store


*Checkers
objStore.hasKey(key);
objStore.hasValue(obj);
objStore.isEmpty();


*Getters
objStore.getValueByKey(key);
objStore.getValueByIndex(arrayIndex);
objStore.getCount();
objStore.getKeys();   // Get the keys for the hash table
objStore.getArray();   // Get the array which stores the objects
objStore.getHash();   // Get the hash table
objStore.getKeyProperty();   // Get the key property (the parameter in the constructor)


*Mischellous
objStore.setKeyProperty(keyProperty);    // Set the key property (the parameter in the constructor)
objStore.each(callback);   // Similar to jquery.each
objStore.find(finder); // Pass a finder function and it will return an object store which contains the elements which finder returns true on
objStore.filter(filter);  // Pass in a filter function and if this function returns true, the corresponding element will be removed
objStore.sort(comparer);    // Sort the internal array
objStore.sortByProperty(sortProperty);    // Sort the internal arrary by the specified property name
objStore.sortByKey();   // Sort the internal array by the key property
objStore.clone();   // Returns another object store with the same set of elements

Last edited Dec 28, 2011 at 12:42 PM by imgen, version 9

Comments

No comments yet.