Permalink
Browse files

Added `destroyAll` method to DbQueryCollections to remove all the items

in the collection.
  • Loading branch information...
1 parent 17c9ab7 commit a2c07b57f5bceaea6756ba3909e0ae66bbb762b8 @zefhemel zefhemel committed Jun 23, 2010
Showing with 48 additions and 1 deletion.
  1. +5 −0 README.md
  2. +43 −1 persistence.js
View
@@ -302,6 +302,11 @@ A `QueryCollection` has the following methods:
Adds object `obj` to the collection.
* `remove(obj)`
Removes object `obj` from the collection.
+* `one(tx, callback)`
+ Asynchronously fetches the first element of the collection, or `null` if none.
+* `destroyAll(tx, callback)`
+ Asynchronously removes all the items in the collection. __Important__: this does
+ not only remove the items from the collection, but removes the items themselves!
Query collections are returned by:
View
@@ -510,7 +510,16 @@ var persistence = (window && window.persistence) ? window.persistence : {};
var coll = it;
if (meta.hasMany[coll].manyToMany) {
that.__defineSetter__(coll, function (val) {
- throw "Not yet supported!!!";
+ if(val && val._items) {
+ // Local query collection, just add each item
+ // TODO: this is technically not correct, should clear out existing items too
+ var items = val._items;
+ for(var i = 0; i < items.length; i++) {
+ that[coll].add(items[i]);
+ }
+ } else {
+ throw "Not yet supported.";
+ }
});
that.__defineGetter__(coll,
function () {
@@ -1438,6 +1447,39 @@ var persistence = (window && window.persistence) ? window.persistence : {};
};
/**
+ * Asynchronous call to remove all the items in the collection.
+ * Note: does not only remove the items from the collection, but
+ * the items themselves.
+ * @param tx transaction to use
+ * @param callback function to be called when clearing has completed
+ */
+ DbQueryCollection.prototype.destroyAll = function (tx, callback) {
+ var that = this;
+ if(tx && !tx.executeSql) { // provided callback as first argument
+ callback = tx;
+ tx = null;
+ }
+ if(!tx) { // no transaction supplied
+ persistence.transaction(function(tx) {
+ that.clear(tx, callback);
+ });
+ return;
+ }
+ var entityName = this._entityName;
+
+ var args = [];
+ var whereSql = "WHERE "
+ + [ this._filter.sql("", args) ].concat(
+ this._additionalWhereSqls).join(' AND ');
+
+ var sql = "DELETE FROM `" + entityName + "` " + whereSql;
+
+ persistence.flush(tx, function () {
+ tx.executeSql(sql, args, callback);
+ });
+ };
+
+ /**
* An implementation of QueryCollection, that is used
* to represent all instances of an entity type
* @constructor

0 comments on commit a2c07b5

Please sign in to comment.