MCPcopy
hub / github.com/jeromegn/Backbone.localStorage / sync

Function sync

src/sync.js:26–82  ·  view source on GitHub ↗
(method, model, options = {})

Source from the content-addressed store, hash-verified

24 * @returns {undefined}
25 */
26export function sync(method, model, options = {}) {
27 const store = getLocalStorage(model);
28 let resp, errorMessage;
29 const syncDfd = getDeferred();
30
31 try {
32 switch (method) {
33 case 'read':
34 resp = isUndefined(model.id) ? store.findAll() : store.find(model);
35 break;
36 case 'create':
37 resp = store.create(model);
38 break;
39 case 'patch':
40 case 'update':
41 resp = store.update(model);
42 break;
43 case 'delete':
44 resp = store.destroy(model);
45 break;
46 }
47
48 } catch (error) {
49 if (error.code === 22 && store._storageSize() === 0) {
50 errorMessage = 'Private browsing is unsupported';
51 } else {
52 errorMessage = error.message;
53 }
54 }
55
56 if (resp) {
57 if (options.success) {
58 options.success.call(model, resp, options);
59 }
60 if (syncDfd) {
61 syncDfd.resolve(resp);
62 }
63
64 } else {
65 errorMessage = errorMessage ? errorMessage : 'Record Not Found';
66
67 if (options.error) {
68 options.error.call(model, errorMessage, options);
69 }
70 if (syncDfd) {
71 syncDfd.reject(errorMessage);
72 }
73 }
74
75 // add compatibility with $.ajax
76 // always execute callback for success and error
77 if (options.complete) {
78 options.complete.call(model, resp);
79 }
80
81 return syncDfd && syncDfd.promise();
82}

Callers

nothing calls this directly

Calls 8

getLocalStorageFunction · 0.90
getDeferredFunction · 0.85
findAllMethod · 0.80
findMethod · 0.80
createMethod · 0.80
updateMethod · 0.80
destroyMethod · 0.80
_storageSizeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…