canmove, Confirmed users
725
edits
Line 58: | Line 58: | ||
== Writing a Store class == | == Writing a Store class == | ||
Your Store class (which extends <tt>Store</tt>, as defined in <tt> | 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:// | 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> | ||
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, | 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 | |||
=== 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) { | ||
record = new FooRecord(uri); | |||
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; | ||
// return the record | // return the record | ||
return record; | return record; |