Sample app to compare accessing an Arrow API using the sample HTTPClient example provided in the auto generated docs to using the generated Titanium Client SDK
Last active
July 28, 2016 19:16
Star
You must be signed in to star a gist
Appcelerator Arrow Client SDK Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is your global styles file. Selectors and rules you define | |
here will be applied throughout your app. However, these rules | |
have the lowest priority of any style settings. | |
For more information, see the "Style Priorities" section of | |
http://docs.appcelerator.com/platform/latest/#!/guide/Alloy_Styles_and_Themes | |
For example, the following would apply to all labels, windows, | |
and text fields (depending on platform) in your app unless you | |
overrode the settings with other TSS, XML, or JS settings: | |
'Label[platform=android,windows]': { | |
color: '#000' // all platforms except Android and Windows default to black | |
} | |
'Window': { | |
backgroundColor: '#fff' // white background instead of default transparent or black | |
} | |
'TextField[platform=android]': { | |
height: Ti.UI.SIZE | |
} | |
*/ | |
'Window[platform=android]': { | |
windowSoftInputMode: Titanium.UI.Android.SOFT_INPUT_STATE_HIDDEN | |
} | |
'Label': { | |
color: "black" | |
} | |
'TextField': { | |
height: 44, | |
width: "80%", | |
borderColor: "gray", | |
leftButtonPadding: 10, | |
color: "black" | |
} | |
'TableViewRow': { | |
height: 44 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function clearClick(e) { | |
Ti.API.debug("clearClick()"); | |
var rows = []; | |
$.accountTV.setData(rows); | |
} | |
function getClickSDK(e) { | |
Ti.API.debug("getClickSDK()"); | |
if(Titanium.Network.networkType == Titanium.Network.NETWORK_NONE){ | |
Ti.API.debug("No Network"); | |
return; | |
} | |
var Lbsfdemoapi = require('Lbsfdemoapi'); | |
Lbsfdemoapi.domain = Alloy.CFG.arrowBaseURL; | |
var Account = Lbsfdemoapi.getModel('Account'); | |
Account.findAll(function(err, results, client) { | |
if(err) { | |
console.log(err); | |
} else { | |
console.log(results); | |
Ti.API.debug("results = "+JSON.stringify(results)); | |
fillTable(results); | |
} | |
}); | |
} | |
function getClickHTTP(e) { | |
Ti.API.debug("getClickHTTP()"); | |
if(Titanium.Network.networkType == Titanium.Network.NETWORK_NONE){ | |
Ti.API.debug("No Network"); | |
return; | |
} | |
var xhr = Ti.Network.createHTTPClient({ | |
onload: function onLoad() { | |
// alert("Loaded: " + this.status + ": " + this.responseText); | |
Ti.API.debug("this.responseText = "+this.responseText); | |
var results = JSON.parse(this.responseText); | |
fillTable(results.accounts); | |
}, | |
onerror: function onError() { | |
alert("Errored: " + this.status + ": " + this.responseText); | |
} | |
}); | |
xhr.open("GET",Alloy.CFG.arrowBaseURL+"/api/account"); | |
xhr.send(); | |
} | |
function fillTable(e) { | |
Ti.API.debug("fillTable(), e = "+e); | |
var rows = []; | |
if(e.length>0){ | |
_.each(e, function(item) { | |
rows.push(Alloy.createController('row', { | |
name: item.Name | |
}).getView()); | |
}); | |
} | |
else { | |
alert("No results found, please try again"); | |
} | |
$.accountTV.setData(rows); | |
} | |
$.index.open(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
".container": { | |
backgroundColor:"white" | |
} | |
"Label": { | |
width: Ti.UI.SIZE, | |
height: Ti.UI.SIZE, | |
color: "#000" | |
} | |
"#label": { | |
font: { | |
fontSize: 12 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Alloy> | |
<Window class="container" layout="vertical"> | |
<Label top="50" id="label">ACCOUNTS</Label> | |
<Button top="10" onClick="getClickSDK">Get Accounts (Client SDK)</Button> | |
<Button top="10" onClick="getClickHTTP">Get Accounts (HTTPClient)</Button> | |
<Button top="10" onClick="clearClick">Clear Table</Button> | |
<TableView top="10" id="accountTV" /> | |
</Window> | |
</Alloy> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = { | |
headers: {}, | |
timeout: 30000, | |
domain: 'http://localhost', | |
port: 0 | |
}; | |
var NoCache = 1, | |
CacheElseNetwork = 2, | |
NetworkElseCache = 4, | |
CacheThenNetwork = 8, | |
CacheOnly = 16, | |
NetworkOnly = 32; | |
var SDK = module.exports = { | |
/* | |
Public API. | |
*/ | |
getModel: function (name, failIfMissing) { | |
name = String(name || '').replace('connector/', ''); | |
var result = this.models[name]; | |
if (!result) { | |
for (var modelName in this.models) { | |
if (this.models.hasOwnProperty(modelName) && name === modelName.split('/').pop()) { | |
result = this.models[modelName]; | |
break; | |
} | |
} | |
} | |
if (!result && failIfMissing) { | |
throw new Error('Couldn\'t find model named: ' + name); | |
} | |
return result; | |
}, | |
getAPI: function (pathOrNickname, method) { | |
for (var i = 0; i < this.apis.length; i++) { | |
var API = this.apis[i]; | |
if (1 === arguments.length) { | |
if (API.nickname === pathOrNickname) { | |
return API; | |
} | |
} | |
else { | |
if (API.path === pathOrNickname && API.method === method) { | |
return API; | |
} | |
} | |
} | |
return null; | |
}, | |
/* | |
Constants. | |
*/ | |
Policy: { | |
/** | |
* No caches are used or saved. This is the default caching policy for models. | |
*/ | |
NoCache: 1, | |
/** | |
* If a local cache of the results is available, it will be used; otherwise, the network will be used. Results will | |
* be cached. | |
*/ | |
CacheElseNetwork: 2, | |
/** | |
* If the network is available, it will be used for results; otherwise, the local cache will be used. Results will | |
* be cached. | |
*/ | |
NetworkElseCache: 4, | |
/** | |
* If a local cache of the result is available, it will be used immediately, and then the network will be used. The | |
* callback will, thus, be called twice. Results will be cached. | |
*/ | |
CacheThenNetwork: 8, | |
/** | |
* If a local cache of results is available, it will be used. | |
*/ | |
CacheOnly: 16, | |
/** | |
* If the network is available, it will be used for results. Results will be cached. | |
*/ | |
NetworkOnly: 32 | |
}, | |
/* | |
Configuration. | |
*/ | |
cachePolicy: {type: NoCache}, | |
get domain() { return config.domain; }, | |
set domain(val) { config.domain = val; }, | |
get port() { return config.port; }, | |
set port(val) { config.port = val; }, | |
get timeout() { return config.timeout; }, | |
set timeout(val) { config.timeout = val; }, | |
get APIKey() { return config.APIKey; }, | |
set APIKey(val) { | |
config.APIKey = val; | |
config.headers.APIKey = val; | |
}, | |
get Authorization() { return config.Authorization; }, | |
set Authorization(val) { | |
config.Authorization = val; | |
if (0 === val.indexOf('Basic ')) { | |
config.headers.Authorization = val; | |
} | |
else if (':' === val.slice(-1)) { | |
config.headers.Authorization = 'Basic ' + Ti.Utils.base64encode(val); | |
} | |
else { | |
config.headers.Authorization = 'Basic ' + Ti.Utils.base64encode(val + ':'); | |
} | |
}, | |
/* | |
Utility. | |
*/ | |
isFunction: function isFunction(val) { | |
return 'function' === typeof val; | |
}, | |
appendQuery: function appendQuery(url, query) { | |
for (var queryName in query) { | |
if (query.hasOwnProperty(queryName)) { | |
url += (url.indexOf('?') === -1 ? '?' : '&') + queryName; | |
if (query[queryName] !== '') { | |
url += '=' + encodeURIComponent(query[queryName]); | |
} | |
} | |
} | |
return url; | |
}, | |
constructHTTP: function constructHTTP(method, url, onLoad, onError) { | |
var client = Ti.Network.createHTTPClient({ | |
onload: onLoad, | |
onerror: onError, | |
timeout: config.timeout | |
}); | |
client.open(method, url); | |
// Add headers. | |
for (var headerName in config.headers) { | |
if (config.headers.hasOwnProperty(headerName)) { | |
client.setRequestHeader(headerName, config.headers[headerName]); | |
} | |
} | |
return client; | |
}, | |
getURL: function getURL(path) { | |
var url = config.domain; | |
if (config.port && config.port !== 80) { | |
url += ':' + config.port; | |
} | |
url += path; | |
return url; | |
}, | |
/** | |
* Turns a url and method in to a hash that can uniquely and securely identify it. | |
*/ | |
getCacheHash: function getCacheHash(method, url) { | |
var key = method + ':' + url + '\n'; | |
// Add headers. | |
for (var headerName in config.headers) { | |
if (config.headers.hasOwnProperty(headerName)) { | |
key += headerName + ':' + config.headers[headerName] + '\n'; | |
} | |
} | |
return Ti.Utils.sha256(key); | |
}, | |
/** | |
* Gets the cache db, ensuring it has the proper schema set up. | |
* @returns {Window} | |
*/ | |
getCacheDB: function () { | |
if (this.cacheDB) { | |
return Ti.Database.open(this.cacheDB); | |
} | |
this.cacheDB = 'LbsfdemoapiCache'; | |
var db = Ti.Database.open(this.cacheDB); | |
db.execute('CREATE TABLE IF NOT EXISTS cache(hash TEXT PRIMARY KEY, until INTEGER, result BLOB);'); | |
db.file.setRemoteBackup(false); | |
return db; | |
}, | |
/** | |
* Checks the cache to see if results have been cached for the specific request. | |
* @param hash A string that uniquely identifies the request. | |
* @returns The cached results, or undefined. | |
*/ | |
readCache: function (hash) { | |
var db = this.getCacheDB(); | |
db.execute('DELETE FROM cache WHERE until < ?', Date.now()); | |
var result = db.execute('SELECT result FROM cache WHERE hash = ? LIMIT 1', hash), | |
retVal; | |
if (result.isValidRow()) { | |
retVal = JSON.parse(result.fieldByName('result')); | |
} | |
result.close(); | |
db.close(); | |
return retVal; | |
}, | |
/** | |
* | |
* @param hash | |
* @param duration | |
* @param results | |
*/ | |
writeCache: function (hash, duration, results) { | |
var db = this.getCacheDB(); | |
db.execute('INSERT OR REPLACE INTO cache (hash, until, result) VALUES (?, ?, ?);', | |
hash, | |
Date.now() + duration, | |
JSON.stringify(results)); | |
db.close(); | |
}, | |
/* | |
Definitions. | |
*/ | |
models: { | |
'AccountWithOpportunity': { | |
cachePolicy: { | |
duration: 360000, | |
type: NoCache | |
}, | |
'create': function method(body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/accountwithopportunity'); | |
var client = SDK.constructHTTP('POST', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
body = callback = url = client = null; | |
} | |
}, | |
'deleteAll': function method(callback, options) { | |
var url = SDK.getURL('/api/accountwithopportunity'); | |
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
callback = url = client = null; | |
} | |
}, | |
'findAll': function method(callback, options) { | |
var url = SDK.getURL('/api/accountwithopportunity'); | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
callback = url = client = null; | |
} | |
}, | |
'query': function method(query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/accountwithopportunity/query'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = callback = url = client = null; | |
} | |
}, | |
'count': function method(query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/accountwithopportunity/count'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = callback = url = client = null; | |
} | |
}, | |
'upsert': function method(body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/accountwithopportunity/upsert'); | |
var client = SDK.constructHTTP('POST', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
body = callback = url = client = null; | |
} | |
}, | |
'findAndModify': function method(query, body, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
body = {}; | |
} | |
else if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/accountwithopportunity/findAndModify'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var client = SDK.constructHTTP('PUT', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = body = callback = url = client = null; | |
} | |
}, | |
'findByID': function method(id, callback, options) { | |
var url = SDK.getURL('/api/accountwithopportunity/:id'); | |
url = url.replace(':id', id); | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = callback = url = client = null; | |
} | |
}, | |
'delete': function method(id, callback, options) { | |
var url = SDK.getURL('/api/accountwithopportunity/:id'); | |
url = url.replace(':id', id); | |
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = callback = url = client = null; | |
} | |
}, | |
'update': function method(id, body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/accountwithopportunity/:id'); | |
url = url.replace(':id', id); | |
var client = SDK.constructHTTP('PUT', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = body = callback = url = client = null; | |
} | |
}, | |
'distinct': function method(field, query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/accountwithopportunity/distinct/:field'); | |
url = url.replace(':field', field); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
field = query = callback = url = client = null; | |
} | |
} | |
}, | |
'Account': { | |
cachePolicy: { | |
duration: 360000, | |
type: NoCache | |
}, | |
'query': function method(query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/account/query'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = callback = url = client = null; | |
} | |
}, | |
'upsert': function method(body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/account/upsert'); | |
var client = SDK.constructHTTP('POST', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
body = callback = url = client = null; | |
} | |
}, | |
'create': function method(body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/account'); | |
var client = SDK.constructHTTP('POST', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
body = callback = url = client = null; | |
} | |
}, | |
'count': function method(query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/account/count'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = callback = url = client = null; | |
} | |
}, | |
'findAll': function method(callback, options) { | |
var url = SDK.getURL('/api/account'); | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
callback = url = client = null; | |
} | |
}, | |
'deleteAll': function method(callback, options) { | |
var url = SDK.getURL('/api/account'); | |
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
callback = url = client = null; | |
} | |
}, | |
'findAndModify': function method(query, body, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
body = {}; | |
} | |
else if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/account/findAndModify'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var client = SDK.constructHTTP('PUT', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = body = callback = url = client = null; | |
} | |
}, | |
'update': function method(id, body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/account/:id'); | |
url = url.replace(':id', id); | |
var client = SDK.constructHTTP('PUT', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = body = callback = url = client = null; | |
} | |
}, | |
'findByID': function method(id, callback, options) { | |
var url = SDK.getURL('/api/account/:id'); | |
url = url.replace(':id', id); | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = callback = url = client = null; | |
} | |
}, | |
'delete': function method(id, callback, options) { | |
var url = SDK.getURL('/api/account/:id'); | |
url = url.replace(':id', id); | |
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = callback = url = client = null; | |
} | |
}, | |
'distinct': function method(field, query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/account/distinct/:field'); | |
url = url.replace(':field', field); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
field = query = callback = url = client = null; | |
} | |
} | |
}, | |
'Opportunity': { | |
cachePolicy: { | |
duration: 360000, | |
type: NoCache | |
}, | |
'query': function method(query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/opportunity/query'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = callback = url = client = null; | |
} | |
}, | |
'upsert': function method(body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/opportunity/upsert'); | |
var client = SDK.constructHTTP('POST', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
body = callback = url = client = null; | |
} | |
}, | |
'count': function method(query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/opportunity/count'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = callback = url = client = null; | |
} | |
}, | |
'findAll': function method(callback, options) { | |
var url = SDK.getURL('/api/opportunity'); | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
callback = url = client = null; | |
} | |
}, | |
'deleteAll': function method(callback, options) { | |
var url = SDK.getURL('/api/opportunity'); | |
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
callback = url = client = null; | |
} | |
}, | |
'create': function method(body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/opportunity'); | |
var client = SDK.constructHTTP('POST', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
body = callback = url = client = null; | |
} | |
}, | |
'findAndModify': function method(query, body, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
body = {}; | |
} | |
else if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/opportunity/findAndModify'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var client = SDK.constructHTTP('PUT', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = body = callback = url = client = null; | |
} | |
}, | |
'delete': function method(id, callback, options) { | |
var url = SDK.getURL('/api/opportunity/:id'); | |
url = url.replace(':id', id); | |
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = callback = url = client = null; | |
} | |
}, | |
'findByID': function method(id, callback, options) { | |
var url = SDK.getURL('/api/opportunity/:id'); | |
url = url.replace(':id', id); | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = callback = url = client = null; | |
} | |
}, | |
'update': function method(id, body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/opportunity/:id'); | |
url = url.replace(':id', id); | |
var client = SDK.constructHTTP('PUT', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = body = callback = url = client = null; | |
} | |
}, | |
'distinct': function method(field, query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/opportunity/distinct/:field'); | |
url = url.replace(':field', field); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
field = query = callback = url = client = null; | |
} | |
} | |
} | |
}, | |
apis: [ | |
{ | |
nickname: 'Query', | |
path: '/api/opportunity/query', | |
method: 'GET', | |
execute: function method(query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/opportunity/query'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Upsert', | |
path: '/api/opportunity/upsert', | |
method: 'POST', | |
execute: function method(body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/opportunity/upsert'); | |
var client = SDK.constructHTTP('POST', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
body = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Count', | |
path: '/api/opportunity/count', | |
method: 'GET', | |
execute: function method(query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/opportunity/count'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Find All', | |
path: '/api/opportunity', | |
method: 'GET', | |
execute: function method(callback, options) { | |
var url = SDK.getURL('/api/opportunity'); | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Delete All', | |
path: '/api/opportunity', | |
method: 'DELETE', | |
execute: function method(callback, options) { | |
var url = SDK.getURL('/api/opportunity'); | |
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Create', | |
path: '/api/opportunity', | |
method: 'POST', | |
execute: function method(body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/opportunity'); | |
var client = SDK.constructHTTP('POST', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
body = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Find and Modify', | |
path: '/api/opportunity/findAndModify', | |
method: 'PUT', | |
execute: function method(query, body, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
body = {}; | |
} | |
else if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/opportunity/findAndModify'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var client = SDK.constructHTTP('PUT', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = body = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Delete One', | |
path: '/api/opportunity/:id', | |
method: 'DELETE', | |
execute: function method(id, callback, options) { | |
var url = SDK.getURL('/api/opportunity/:id'); | |
url = url.replace(':id', id); | |
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Find By ID', | |
path: '/api/opportunity/:id', | |
method: 'GET', | |
execute: function method(id, callback, options) { | |
var url = SDK.getURL('/api/opportunity/:id'); | |
url = url.replace(':id', id); | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Update', | |
path: '/api/opportunity/:id', | |
method: 'PUT', | |
execute: function method(id, body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/opportunity/:id'); | |
url = url.replace(':id', id); | |
var client = SDK.constructHTTP('PUT', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = body = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Distinct', | |
path: '/api/opportunity/distinct/:field', | |
method: 'GET', | |
execute: function method(field, query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/opportunity/distinct/:field'); | |
url = url.replace(':field', field); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
field = query = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Create', | |
path: '/api/accountwithopportunity', | |
method: 'POST', | |
execute: function method(body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/accountwithopportunity'); | |
var client = SDK.constructHTTP('POST', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
body = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Delete All', | |
path: '/api/accountwithopportunity', | |
method: 'DELETE', | |
execute: function method(callback, options) { | |
var url = SDK.getURL('/api/accountwithopportunity'); | |
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Find All', | |
path: '/api/accountwithopportunity', | |
method: 'GET', | |
execute: function method(callback, options) { | |
var url = SDK.getURL('/api/accountwithopportunity'); | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Query', | |
path: '/api/accountwithopportunity/query', | |
method: 'GET', | |
execute: function method(query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/accountwithopportunity/query'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Count', | |
path: '/api/accountwithopportunity/count', | |
method: 'GET', | |
execute: function method(query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/accountwithopportunity/count'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Upsert', | |
path: '/api/accountwithopportunity/upsert', | |
method: 'POST', | |
execute: function method(body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/accountwithopportunity/upsert'); | |
var client = SDK.constructHTTP('POST', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
body = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Find and Modify', | |
path: '/api/accountwithopportunity/findAndModify', | |
method: 'PUT', | |
execute: function method(query, body, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
body = {}; | |
} | |
else if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/accountwithopportunity/findAndModify'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var client = SDK.constructHTTP('PUT', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = body = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Find By ID', | |
path: '/api/accountwithopportunity/:id', | |
method: 'GET', | |
execute: function method(id, callback, options) { | |
var url = SDK.getURL('/api/accountwithopportunity/:id'); | |
url = url.replace(':id', id); | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Delete One', | |
path: '/api/accountwithopportunity/:id', | |
method: 'DELETE', | |
execute: function method(id, callback, options) { | |
var url = SDK.getURL('/api/accountwithopportunity/:id'); | |
url = url.replace(':id', id); | |
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Update', | |
path: '/api/accountwithopportunity/:id', | |
method: 'PUT', | |
execute: function method(id, body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/accountwithopportunity/:id'); | |
url = url.replace(':id', id); | |
var client = SDK.constructHTTP('PUT', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = body = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Distinct', | |
path: '/api/accountwithopportunity/distinct/:field', | |
method: 'GET', | |
execute: function method(field, query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/accountwithopportunity/distinct/:field'); | |
url = url.replace(':field', field); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
field = query = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Query', | |
path: '/api/account/query', | |
method: 'GET', | |
execute: function method(query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/account/query'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Upsert', | |
path: '/api/account/upsert', | |
method: 'POST', | |
execute: function method(body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/account/upsert'); | |
var client = SDK.constructHTTP('POST', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
body = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Create', | |
path: '/api/account', | |
method: 'POST', | |
execute: function method(body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/account'); | |
var client = SDK.constructHTTP('POST', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
body = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Count', | |
path: '/api/account/count', | |
method: 'GET', | |
execute: function method(query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/account/count'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Find All', | |
path: '/api/account', | |
method: 'GET', | |
execute: function method(callback, options) { | |
var url = SDK.getURL('/api/account'); | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Delete All', | |
path: '/api/account', | |
method: 'DELETE', | |
execute: function method(callback, options) { | |
var url = SDK.getURL('/api/account'); | |
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Find and Modify', | |
path: '/api/account/findAndModify', | |
method: 'PUT', | |
execute: function method(query, body, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
body = {}; | |
} | |
else if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/account/findAndModify'); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var client = SDK.constructHTTP('PUT', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
query = body = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Update', | |
path: '/api/account/:id', | |
method: 'PUT', | |
execute: function method(id, body, callback, options) { | |
if (SDK.isFunction(body)) { | |
callback = body; | |
body = {}; | |
} | |
var url = SDK.getURL('/api/account/:id'); | |
url = url.replace(':id', id); | |
var client = SDK.constructHTTP('PUT', url, onLoad, onError); | |
client.send(body); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = body = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Find By ID', | |
path: '/api/account/:id', | |
method: 'GET', | |
execute: function method(id, callback, options) { | |
var url = SDK.getURL('/api/account/:id'); | |
url = url.replace(':id', id); | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Delete One', | |
path: '/api/account/:id', | |
method: 'DELETE', | |
execute: function method(id, callback, options) { | |
var url = SDK.getURL('/api/account/:id'); | |
url = url.replace(':id', id); | |
var client = SDK.constructHTTP('DELETE', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
id = callback = url = client = null; | |
} | |
} | |
}, | |
{ | |
nickname: 'Distinct', | |
path: '/api/account/distinct/:field', | |
method: 'GET', | |
execute: function method(field, query, callback, options) { | |
if (SDK.isFunction(query)) { | |
callback = query; | |
query = {}; | |
} | |
var url = SDK.getURL('/api/account/distinct/:field'); | |
url = url.replace(':field', field); | |
if (query) { | |
url = SDK.appendQuery(url, query); | |
} | |
var cachePolicy = (options && options.cachePolicy) || this.cachePolicy || SDK.cachePolicy, | |
cacheHash = SDK.getCacheHash('GET', url); | |
// If we're using one of the policies that can use the cache... | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.CacheThenNetwork | SDK.Policy.CacheOnly))) { | |
var cached = SDK.readCache(cacheHash); | |
if (cached) { | |
callback && callback(null, cached, null); | |
// CacheElseNetwork or CacheOnly? Then we can return. | |
if (cachePolicy.type !== SDK.Policy.CacheThenNetwork) { | |
clean(); | |
return cached; | |
} | |
// CacheThenNetwork would carry on. | |
} | |
// CacheOnly will never hit the network; it wants to return, even if we don't find results. | |
if (cachePolicy.type === SDK.Policy.CacheOnly) { | |
callback && callback(new Error('No results have been cached yet.'), null, null); | |
clean(); | |
return; | |
} | |
} | |
var client = SDK.constructHTTP('GET', url, onLoad, onError); | |
client.send(); | |
function onLoad() { | |
if (!callback) { | |
return; | |
} | |
var body = this.responseText; | |
if (/^application\/(.*\\+)?json/.test(this.getResponseHeader('Content-Type'))) { | |
try { | |
body = JSON.parse(body); | |
if (body.key && body[body.key] !== undefined) { | |
body = body[body.key]; | |
} | |
} | |
catch (e) { | |
console.error('Failed to parse the body from the server:'); | |
console.error(body); | |
console.error(e); | |
} | |
} | |
if (this.status >= 200 && this.status <= 299) { | |
if (!!(cachePolicy.type & (SDK.Policy.CacheElseNetwork | SDK.Policy.NetworkElseCache | SDK.Policy.CacheThenNetwork | SDK.Policy.NetworkOnly))) { | |
SDK.writeCache(cacheHash, cachePolicy.duration, body); | |
// TODO: When we do a findAll, cache findByID, too. | |
// TODO: Non-GETs should bust the cache for find, right? | |
} | |
callback(null, body, client); | |
} | |
else { | |
callback(body, null, client); | |
} | |
clean(); | |
} | |
function onError(e) { | |
if (!callback) { | |
return; | |
} | |
if (!!(cachePolicy.type & (SDK.Policy.NetworkElseCache))) { | |
var cached = SDK.readCache(url); | |
if (cached) { | |
callback(null, cached, null); | |
clean(); | |
return; | |
} | |
} | |
callback(e.error, null, client); | |
clean(); | |
} | |
function clean() { | |
field = query = callback = url = client = null; | |
} | |
} | |
} | |
] | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var args = arguments[0] || {}; | |
Ti.API.debug("args.name = "+args.name); | |
$.accountNameLbl.text = args.name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Alloy> | |
<TableViewRow class="accountTVR"> | |
<Label class="rowLbl" id="accountNameLbl" left="10"/> | |
</TableViewRow> | |
</Alloy> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment