Repositories
Repository is intended to store your domain objects.
To create one, you'd do the following:
include(bbq.domain.Repository);
include(myapp.domain.Room);
var repository = new bbq.domain.Repository({
// This is the class that this Repository will hold
type: myapp.domain.Room,
// An optional argument which holds entities to pre-fill the repo with
entities: []
});
You can then call get and add (among others) to access objects stored in the repository.
See the JSDocs for a complete list.
Declaring your repositories
There should only be one repository instance per domain class on a given page. Global variables are bad so the recommended place to define them is in the currentPage
variable.
myapp.page.Home = new Class.create(bbq.page.Page, {
initialize: function($super, args) {
try {
$super(args);
this.rooms = new bbq.domain.Repository({
type: myapp.domain.Room
});
this.bookings = new bbq.domain.Repository({
type: myapp.domain.Booking
});
this.users = new bbq.domain.Repository({
type: myapp.domain.User
});
} catch(e) {
Log.error("Error constructing Home", e);
}
}
});
Repositories can then be accessed like so:
var room = currentPage.rooms.add({id: 129});
add
add allows you to add domain objects to the repository in three ways - by passing in a fully fledged object, a data object or an identifier.
include(bbq.domain.Repository);
include(myapp.domain.Room);
var repository = new bbq.domain.Repository({
// This is the class that this Repository will hold
type: myapp.domain.Room
});
- Adding via id
var room = repository.add(298);
// is the same as doing
room = repository.add(new myapp.domain.Room({id: 298}));
- Adding via data object
var room = repository.add({id: 298});
// is the same as doing
room = repository.add(new myapp.domain.Room({id: 298}));
- Adding via domain object
var room = repository.add(new myapp.domain.Room({id: 298}));
Domain objects will not be duplicated so it is safe to call add several times:
// returns 0
repository.size();
// add an Entity
repository.add(298);
// returns 1
repository.size();
// add it again
repository.add(298);
// still returns 1
repository.size();
// add it again via different method
repository.add(new myapp.domain.Room({id: 298}));
// still 1
repository.size();