Skip to content

Instantly share code, notes, and snippets.

@rolaveric
Last active August 29, 2015 13:57
Show Gist options
  • Save rolaveric/9463376 to your computer and use it in GitHub Desktop.
Save rolaveric/9463376 to your computer and use it in GitHub Desktop.
Example of gopherjsModelOriginal.js refactored to better match a Go API.
// 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