(method, model, options = {})
| 24 | * @returns {undefined} |
| 25 | */ |
| 26 | export 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…