Skip to content

Instantly share code, notes, and snippets.

@nirmaljpatel
Created March 21, 2016 21:07
Show Gist options
  • Save nirmaljpatel/7bbc891966f0bd1dbee5 to your computer and use it in GitHub Desktop.
Save nirmaljpatel/7bbc891966f0bd1dbee5 to your computer and use it in GitHub Desktop.
Extending Backbone Model with methods and use them to initialise Alloy Models
Backbone.Model.prototype.generateUniqueId = function(prefix, empNo) {
try {
var timestamp = moment.utc().format("MMDDYYHHmmss");
var randomNumber = (Math.floor(((Math.random() * 10000) + 1)));
randomNumber = (randomNumber < 1000) ? randomNumber + 1000 : randomNumber;
var genId = prefix + "-" + empNo + "-" + timestamp + "-" + randomNumber;
Ti.API.log('BBModel', "AppGenUniqueId: " + genId);
return genId;
} catch(err) {
Ti.API.log('BBModel', "Error generating unique Id "+ prefix +" for " + empNo);
return "";
}
};
exports.definition = {
config : {
columns : {
"Id" : "INTEGER PRIMARY KEY AUTOINCREMENT",
"AppGenFinAgrId" : "TEXT",
"InspectionId" : "TEXT",
//....
},
adapter : {
type : "sql",
collection_name : "FinanceAgreement",
idAttribute: "Id"
}
},
extendModel : function(Model) {
_.extend(Model.prototype, {
// extended functions and properties go here
initialize : function() {
this.set('AppGenFinAgrId', this.generateUniqueId("F", configuration.login.employeeNumber));
}});
return Model;
},
var finAgmt = Alloy.createModel('FinanceAgreement', {});
Ti.API.log('FinMgr',JSON.stringify(finAgmt));
/*
Outputs:
[INFO] : [FINMGR] {"AppGenFinAgrId":"F-13703-032116210645-5700"}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment