Created
September 26, 2012 17:19
-
-
Save pegli/3789307 to your computer and use it in GitHub Desktop.
example use case of afterCollectionCreate
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
// 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