-
-
Save nirmaljpatel/7bbc891966f0bd1dbee5 to your computer and use it in GitHub Desktop.
Extending Backbone Model with methods and use them to initialise Alloy Models
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
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 ""; | |
} | |
}; |
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
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; | |
}, |
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 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