Created
October 3, 2011 01:43
-
-
Save micahasmith/1258260 to your computer and use it in GitHub Desktop.
The Repository Pattern in JavaScript
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
function PeopleRepository() { | |
//when using constructor functions like this one to create objects in js, | |
//always make sure you're doing so with the new keyword | |
//or the scope will be off | |
// | |
//like so: | |
if(!(this instanceof PeopleRepository) { | |
return new PeopleRepository(); | |
//we just enforced new | |
} | |
//create a get function on the member | |
this.Get=function(callback) { | |
$.getJSON("some url",callback); | |
}; | |
} | |
//instance the repo like so (of course using new) | |
var repo = new PeopleRepository(); | |
//call a get | |
repo.Get(function(data) { | |
//we could do something with the data that gets returned here | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
also, rather than taking a callback, you should probably use Promises.