Skip to content

Instantly share code, notes, and snippets.

@pegli
Created September 26, 2012 17:19
Show Gist options
  • Save pegli/3789307 to your computer and use it in GitHub Desktop.
Save pegli/3789307 to your computer and use it in GitHub Desktop.
example use case of afterCollectionCreate
// semi-contrived example: adding a count() function to SQL collections which doesn't
// load all of the models.
// Alloy/lib/alloy/sync/sql.js
function Count() {
var table = model.config.adapter.collection_name;
var sql = 'SELECT * FROM '+table;
var rs = db.execute('SELECT COUNT(*) FROM '+table);
return rs.isValidRow() ? rs.field(0) : 0;
}
module.exports.afterCollectionCreate = function(Collection) {
Collection = Collection || {};
Collection.prototype.count = Count;
return Collection;
}
// test/apps/models/sql/controllers/index.js
// Okay time to show the results. Remember this sync's local Backbone server with persistent store.
books.fetch();
// NEW: How many books do I have now?
var c = books.count();
alert(String.format("There are %d books", c));
// DELETE - destroy triggers the CRUD delete operation
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment