Last active
August 29, 2015 13:57
-
-
Save rolaveric/9463376 to your computer and use it in GitHub Desktop.
Example of gopherjsModelOriginal.js refactored to better match a Go 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
// Namespace created within an IIFE for private scope | |
var user = (function () { | |
// Variable for holding the injected DB interface | |
var DB; | |
// User Type | |
function User(name, id) {/* ... */} | |
return { | |
// Expose a function for setting the DB interface | |
"registerDB": function (db) { | |
DB = db; | |
}, | |
"new": function (name) { | |
// The "DB" type's methods will be capitalised | |
DB.Query('INSERT INTO User (name) VALUES (?)', name); | |
// Lets be a bit more type safe and specify that we expect an int | |
var id = DB.Query('SELECT @@IDENTITY').NextRow().GetInt(0); | |
return new User(name, id); | |
}, | |
"get": function (id) {/* ... */}, | |
"all": function () {/* ... */}, | |
"save": function () {/* ... */} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment