Skip to content

Instantly share code, notes, and snippets.

@micahasmith
Created October 3, 2011 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micahasmith/1258260 to your computer and use it in GitHub Desktop.
Save micahasmith/1258260 to your computer and use it in GitHub Desktop.
The Repository Pattern in JavaScript
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
});
@nfedyashev
Copy link

@micahasmith

It doesn't look like idiomatic implementation of the Repository Pattern.

http://martinfowler.com/eaaCatalog/repository.html

A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. It is not responsible for data retrieving from an external resource. That is DAO's implementation details.

HTH

@tommck
Copy link

tommck commented Jul 5, 2013

also, rather than taking a callback, you should probably use Promises.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment