-
-
Save yomybaby/2934929 to your computer and use it in GitHub Desktop.
Backbone.sync for Titanium HttpClient and Titanium ACS API
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
// http://cloud.appcelerator.com/docs/api/v1/acls/create | |
var Backbone = require('/lib/backbone'), | |
_ = require('/lib/underscore'); | |
var ReviewModel = Backbone.Model.extend({ | |
modelNameForACS : 'Reviews' //just add Model Name of ACS | |
}); | |
var ReviewCollection = Backbone.Collection.extend({ | |
model : ReviewModel, | |
modelNameForACS : 'Reviews' | |
}); | |
//When you use 'Custome objects', add classname | |
var MenuModel = Backbone.Model.extend({ | |
modelNameForACS : 'Objects', | |
classname : 'menu' | |
}); |
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
//Customise Backbone.sync to work with Titanium rather than jQuery | |
// plus work with Appcelerator Cloud Service Titanium API. | |
// Installation : replace backbone sync in backbone.js | |
Backbone.sync = (function() { | |
var methodMap = { | |
'create' : 'POST', | |
'update' : 'PUT', | |
'delete' : 'DELETE', | |
'read' : 'GET' | |
}; | |
var methodMapForACS = { | |
'create': 'create', | |
'read' : 'query', | |
'update': 'update', | |
'delete' : 'remove' | |
}; | |
var idMapForACS = { | |
'Checkins' : 'checkin_id', | |
'PhotoCollections' : 'collection_id', | |
'Photos' : 'photo_id', | |
'Places' : 'place_id', | |
'Posts' : 'post_id', | |
'Reviews' : 'review_id' | |
} | |
return function(method, model, options) { | |
if(model.modelNameForACS){ | |
var params = options || {}; | |
methodForACS = options.methodForACS || methodMapForACS[method], | |
modelNameForACS = model.modelNameForACS, | |
parsePropertyName = modelNameForACS.toLowerCase(), | |
Cloud = require('ti.cloud'); | |
// classname for Custom Objects | |
if(modelNameForACS == 'Objects'){ | |
if(!model.classname) throw new Error('ACS Objects Model/Collection require classname property'); | |
if(method == 'create' || method == 'update'){ | |
Ti.API.info(params.data); | |
params.data = _.extend({ | |
"classname" : model.classname, | |
"fields" : model.toJSON() | |
}, | |
params.data); | |
}else{ | |
params.data = _.extend({classname : model.classname}, params.data); | |
} | |
if(model instanceof Model && !model.isNew()) params.data.id = model.get('id'); | |
parsePropertyName = params.classname || model.classname; | |
} | |
// Ensure that we have the appropriate request data. | |
if (!options.data && model) { | |
if(method == 'create' || method == 'update'){ | |
params.contentType = 'application/json'; | |
params.data = model.toJSON(); | |
} | |
//for Model user 'xxx_id' instead of 'id' | |
if(idMapForACS[model.modelNameForACS] && (method == 'delete' || method == 'update')){ | |
alert(idMapForACS[model.modelNameForACS]); | |
params.data = params.data || {}; | |
params.data[idMapForACS[model.modelNameForACS]] = model.get('id'); | |
if(params.data.id) delete params.data.id; | |
// alert(params.data); | |
} | |
} | |
Ti.API.info('Could.'+modelNameForACS+'.'+methodForACS +' calling with'); | |
Ti.API.info(params.data); | |
if((typeof Cloud[modelNameForACS][methodForACS]) == 'undefined'){ | |
alert(String.format('Cloud.%s.%s dosen\'t exist!!',modelNameForACS,methodForACS)); | |
// var errormsg = String.format('Cloud.%s.%s dosen\'t exist!!',modelNameForACS,methodForACS); | |
// throw new Error(errormsg); | |
return; | |
} | |
Cloud[modelNameForACS][methodForACS](_.extend(params.data || {}), | |
function(e){ | |
var rtnData = e; | |
if (e.success) { | |
if(e.hasOwnProperty(parsePropertyName)){ | |
rtnData = e[parsePropertyName]; | |
if(method == 'create' || method == 'update') rtnData = rtnData[0]; | |
} | |
if(e.meta){ | |
// contiunus extend meta | |
model.meta = _.extend(model.meta || {}, e.meta); | |
} | |
Ti.API.info(rtnData); | |
params.success(rtnData); | |
} else { | |
params.error(e); | |
} | |
} | |
); | |
return; | |
} | |
var xhr = Ti.Network.createHTTPClient({ timeout: 5000 }); | |
var type = methodMap[method], | |
params = _.extend({}, options); | |
//==== Start standard Backbone.sync code ==== | |
// Ensure that we have a URL. | |
if (!options.url) { | |
params.url = getValue(model, 'url') || urlError(); | |
} | |
// Ensure that we have the appropriate request data. | |
if (!options.data && model && (method == 'create' || method == 'update')) { | |
params.contentType = 'application/json'; | |
params.data = JSON.stringify(model.toJSON()); | |
} | |
// For older servers, emulate JSON by encoding the request into an HTML-form. | |
if (Backbone.emulateJSON) { | |
params.contentType = 'application/x-www-form-urlencoded'; | |
params.data = params.data ? { | |
model : params.data | |
} : {}; | |
} | |
// For older servers, emulate HTTP by mimicking the HTTP method with `_method` | |
// And an `X-HTTP-Method-Override` header. | |
if (Backbone.emulateHTTP) { | |
if (type === 'PUT' || type === 'DELETE') { | |
if (Backbone.emulateJSON) | |
params.data._method = type; | |
params.type = 'POST'; | |
params.beforeSend = function(xhr) { | |
xhr.setRequestHeader('X-HTTP-Method-Override', type); | |
}; | |
} | |
} | |
/* not nesseery in Titanium | |
// Don't process data on a non-GET request. | |
if (params.type !== 'GET' && !Backbone.emulateJSON) { | |
params.processData = false; | |
} | |
// Make the request, allowing the user to override any Ajax options. | |
return $.ajax(_.extend(params, options)); | |
*/ | |
//==== End standard Backbone.sync code ==== | |
//Handle success | |
xhr.onload = function() { | |
params.success(JSON.parse(this.responseText)); | |
}; | |
//Handle error | |
xhr.onerror = params.error; | |
//Prepare the request | |
xhr.open(type, params.url); | |
//Add request headers etc. | |
xhr.setRequestHeader('Content-Type', params.contentType); | |
if (params.beforeSend) params.beforeSend(xhr); | |
//Make the request | |
xhr.send(params.data); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment