Changes

Jump to: navigation, search

CloudServices/Sync/FxSync/Developer/ClientAPI

1,187 bytes removed, 21:32, 24 September 2010
Writing a Store class
== Writing a Store class ==
Your Store class (which extends <tt>Store</tt>, as defined in <tt>weaveservices/sync/modules/stores.js</tt>) has the job of creating and maintaining a set of Record objects from the underlying data. The store must also make updates to the underlying data itself, to keep the data up-to-date with the user's latest changes, when the store is instructed to do so.
The majority of the code you write for syncing a new data type will most likely be in the Store class.
Depending of what type of data you're working with, you might already have GUIDs built into your data that you can make use of. If not, you may have to invent your own mapping from data objects to GUIDs. In this case, you will probably find the <tt>makeGUID()</tt> function (in Utils) useful:
Cu.import("resource://weaveservies-sync/util.js");
// ...
let newGuid = Utils.makeGUID();
*<tt>itemExists(id)</tt>
*<tt>createRecord(id, uri)</tt>
*<tt>changeItemId(oldId, newId)</tt>
*<tt>getAllIds()</tt>
*<tt>update(record)</tt>
*<tt>remove(record)</tt>
 
(Additionally, if your Store class is a hierarchy, you must also implement the method <tt>createMetaRecords(guid, items)</tt>.)
You may also find it useful to override other methods of the base class, depending on what you're doing.
=== createRecord ===
The <tt>createRecord( guid, cryptoMetaURL uri )</tt> method gets called by the engine to request a new record for an item with a given GUID. The cryptoMetaURL is the URL to the location of the cryptoMeta record that will be used to encrypt it. It's your Store's responsibility to instantiate a Record object (of the class you defined earlier), assign the given GUID and cryptoMetaURL to it, and return it. It's also strongly recommended that your Store cache the record object. The base class provides the cache, so you can store a record in it like so:  this.cache.put( guid, record ); And you can retrieve a record from it like so:  this.cache.get( guid ); Thus, if createRecord is called requesting a GUID for which you've already instantiated a Record, you can just return the cached Record. Otherwise, createRecord should set all the required data on the Record object to reflect the state of the underlying application data identified by the GUID. It must also store the GUID in the id property of the record before returning it, like so:  // ... record.id = guid; record.encryption = cryptoMetaURL; return record;
=== itemExists(guid) ===
// Return true if an item with given guid exists in the store.
},
createRecord: function(guid, uri) { // Return the cached one if we have it: let record = this.cache.get(guid); if (record) return record; // Otherwise, instantiate: record = new FooRecord(uri);
// Look up the data corresponding to this guid, by the mapping
// you've defined.
record.bar = 17; // or whatever
record.id = guid;
// Add it to the cache:
this.cache.put(guid, record);
// return the record
return record;
Canmove, confirm
725
edits

Navigation menu