CloudServices/Sync/FxSync/Developer/ClientAPI: Difference between revisions

Jump to navigation Jump to search
Line 58: Line 58:
== Writing a Store class ==
== Writing a Store class ==


Your Store class (which extends <tt>Store</tt>, as defined in <tt>weave/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.
Your Store class (which extends <tt>Store</tt>, as defined in <tt>services/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.
The majority of the code you write for syncing a new data type will most likely be in the Store class.
Line 68: Line 68:
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:
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://weave/util.js");
  Cu.import("resource://servies-sync/util.js");
  // ...
  // ...
  let newGuid = Utils.makeGUID();
  let newGuid = Utils.makeGUID();
Line 77: Line 77:


*<tt>itemExists(id)</tt>
*<tt>itemExists(id)</tt>
*<tt>createRecord(id)</tt>
*<tt>createRecord(id, uri)</tt>
*<tt>changeItemId(oldId, newId)</tt>
*<tt>changeItemId(oldId, newId)</tt>
*<tt>getAllIds()</tt>
*<tt>getAllIds()</tt>
Line 84: Line 84:
*<tt>update(record)</tt>
*<tt>update(record)</tt>
*<tt>remove(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.
You may also find it useful to override other methods of the base class, depending on what you're doing.
Line 93: Line 91:
=== createRecord ===
=== createRecord ===


The <tt>createRecord( guid, cryptoMetaURL )</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.
The <tt>createRecord( guid, uri )</tt> method gets called by the engine to request a new record for an item with a given GUID. It's your Store's responsibility to instantiate a Record object (of the class you defined earlier), assign the given GUID, and return 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) ===
=== itemExists(guid) ===
Line 157: Line 136:
     // Return true if an item with given guid exists in the store.
     // Return true if an item with given guid exists in the store.
   },
   },
   createRecord: function(guid) {
   createRecord: function(guid, uri) {
    // Return the cached one if we have it:
     record = new FooRecord(uri);
    let record = this.cache.get(guid);
    if (record)
      return record;
    // Otherwise, instantiate:
     record = new FooRecord();
     // Look up the data corresponding to this guid, by the mapping
     // Look up the data corresponding to this guid, by the mapping
     // you've defined.
     // you've defined.
Line 169: Line 143:
     record.bar = 17; // or whatever
     record.bar = 17; // or whatever
     record.id = guid;
     record.id = guid;
    // Add it to the cache:
    this.cache.put(guid, record);
     // return the record
     // return the record
     return record;
     return record;
canmove, Confirmed users
725

edits

Navigation menu