Skip to content

Commit

Permalink
Merge pull request #1 from leoetlino/v0.2
Browse files Browse the repository at this point in the history
Support both Meteor.Collection.ObjectID() and the default Meteor ID strings
  • Loading branch information
Urigo committed Aug 27, 2014
2 parents aeaad0e + 4d750ae commit bea4643
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
22 changes: 14 additions & 8 deletions modules/ngMeteor-collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ ngMeteorCollections.factory('$collection', ['$q', 'HashKeyCopier',
// Remove items that don't exist in the collection anymore.
angular.forEach(oldItems, function (oldItem) {
var index = newItems.map(function (item) {
return item._id;
}).indexOf(oldItem._id);
return item._id._str;
}).indexOf(oldItem._id._str);
if (index == -1) newItems.remove(oldItem._id);
});
newItems.save(); // Saves all items.
Expand Down Expand Up @@ -71,16 +71,21 @@ AngularMeteorCollection.prototype.save = function save(docs) {
function upsertObject(item, $q) {
var deferred = $q.defer();

item = angular.copy(item); // Makes a deep copy without the $$hashKeys.
item = angular.copy(item);
delete item.$$hashKey;
for (var property in item) {
delete property.$$hashKey;
}

if (item._id) { // Performs an update if the _id property is set.
var item_id = item._id; // Store the _id in temporary variable
delete item._id; // Remove the _id property so that it can be $set using update.
collection.update(item_id, {$set: item}, function (error) {
var objectId = (item_id._str) ? new Meteor.Collection.ObjectID(item_id._str) : item_id;
collection.update(objectId, {$set: item}, function (error) {
if (error) {
deferred.reject(error);
} else {
deferred.resolve({_id: item_id, action: "updated"});
deferred.resolve({_id: objectId, action: "updated"});
}
});
} else { // Performs an insert if the _id property isn't set.
Expand Down Expand Up @@ -134,11 +139,12 @@ AngularMeteorCollection.prototype.remove = function remove(keys) {
if(key._id) {
key = key._id;
}
collection.remove({_id: key}, function (error) {
var objectId = (key._str) ? new Meteor.Collection.ObjectID(key._str) : key;
collection.remove(objectId, function (error) {
if (error) {
deferred.reject(error);
} else {
deferred.resolve({_id: key, action: "removed"});
deferred.resolve({_id: objectId, action: "removed"});
}
});
} else {
Expand Down Expand Up @@ -166,4 +172,4 @@ AngularMeteorCollection.prototype.remove = function remove(keys) {
}

return $q.all(promises); // Returns all promises when they're resolved.
};
};
4 changes: 3 additions & 1 deletion package.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Package.describe({
Package.on_use(function (api) {
// Exports the ngMeteor package scope
api.export('ngMeteor', 'client');


api.use('jquery', 'client');

// Files to load in Client only.
api.add_files([
// Lib Files
Expand Down

0 comments on commit bea4643

Please sign in to comment.