Skip to content

Commit c095ac1

Browse files
committed
Merge branch 'master' of github.com:Urigo/angular-meteor
2 parents 91cd122 + ce7bab2 commit c095ac1

File tree

6 files changed

+96
-7
lines changed

6 files changed

+96
-7
lines changed

.docs/angular-meteor/client/views/api/api.AngularMeteorCollection.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
# AngularMeteorCollection
1212

13-
An object that binds a [Meteor Collection](http://docs.meteor.com/#/full/collections) to an AngularJS scope variable. This is the object that return by calling the [$meteor.collection service](http://angular-meteor.com/api/meteorCollection). It represents the documents that exist in the Meteor local collection
13+
An object that binds a [Meteor Collection](http://docs.meteor.com/#/full/collections) to an AngularJS scope variable. This is the object that is returned when [$meteor.collection service](http://angular-meteor.com/api/meteorCollection) is called. It represents the documents that exist in the Meteor local collection.
1414

1515
----
1616

.docs/angular-meteor/client/views/api/api.meteorCollection.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Service binding include 3 parts:
1818

1919
* watching the changes in **Angular $scope** and updating the **Meteor Collection**. (called "autobind")
20-
* watching hte changes in the **Meteor Collection** and updating the **Angular $scope** variables (called "observe")
20+
* watching the changes in the **Meteor Collection** and updating the **Angular $scope** variables (called "observe")
2121

2222
The autobinding is optional, and can be set to false, so changes in the Angular $scope variables will have to be explicitly saved to the collection to be updated to the Meteor Collection.
2323

.docs/angular-meteor/client/views/steps/tutorial.step_09.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ <h1>Step 9 - Privacy and publish-subscribe functions</h1>
6868

6969
* We have `Meteor.publish` - a function to define what to publish from the server to the client
7070
* The first parameter is the name of the subscription. the client will subscribe to that name
71-
* The second parameter is a function the defines what will be returned in the subscription
71+
* The second parameter is a function that defines what will be returned in the subscription
7272

7373
That function will determine what data will be returned and the permissions needed.
7474

modules/angular-meteor-object.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ angularMeteorObject.factory('AngularMeteorObject', ['$q', '$meteorSubscribe', fu
3939
return deferred.promise;
4040
};
4141

42-
AngularMeteorObject.reset = function reset() {
42+
AngularMeteorObject.reset = function reset(keepClientProps) {
4343
var self = this,
4444
collection = self.$$collection,
4545
options = self.$$options,
@@ -48,17 +48,32 @@ angularMeteorObject.factory('AngularMeteorObject', ['$q', '$meteorSubscribe', fu
4848
if (collection){
4949
var serverValue = collection.findOne(id, options);
5050
var prop;
51+
var props;
5152
if (serverValue) {
5253
angular.extend(Object.getPrototypeOf(self), Object.getPrototypeOf(serverValue));
5354
for (prop in serverValue) {
5455
if (serverValue.hasOwnProperty(prop)) {
5556
self[prop] = serverValue[prop];
57+
self._serverBackup[prop] = serverValue[prop];
5658
}
5759
}
60+
61+
if (keepClientProps) {
62+
props = _.intersection(_.keys(self), _.keys(self._serverBackup));
63+
} else {
64+
props = _.keys(self);
65+
}
66+
var serverProps = _.keys(serverValue);
67+
var removedKeys = _.difference(props, serverProps, self.$$internalProps);
68+
_.each(removedKeys, function (prop) {
69+
delete self[prop];
70+
delete self._serverBackup[prop]
71+
});
5872
} else {
5973
for (prop in _.omit(self, self.$$internalProps)) {
6074
delete self[prop];
6175
}
76+
self._serverBackup = {};
6277
}
6378
}
6479
};
@@ -83,15 +98,17 @@ angularMeteorObject.factory('AngularMeteorObject', ['$q', '$meteorSubscribe', fu
8398
// A list of internals properties to not watch for, nor pass to the Document on update and etc.
8499
AngularMeteorObject.$$internalProps = [
85100
'save', 'reset', '$$collection', '$$options', '$$id', '$$hashkey', '$$internalProps', 'subscribe', 'stop', 'autorunComputation', 'unregisterAutoBind', 'unregisterAutoDestroy', 'getRawObject',
86-
'collection', '_eventEmitter'
101+
'collection', '_eventEmitter', '_serverBackup'
87102
];
88103

89104
var createAngularMeteorObject = function(collection, id, options){
90105
// Make data not be an object so we can extend it to preserve
91106
// Collection Helpers and the like
92107
var data = new function SubObject() {};
93-
angular.extend(data, collection.findOne(id, options));
108+
var doc = collection.findOne(id, options)
109+
angular.extend(data, doc);
94110

111+
data._serverBackup = doc || {};
95112
data.$$collection = collection;
96113
data.$$options = options;
97114
data.$$id = id;
@@ -121,7 +138,7 @@ angularMeteorObject.factory('$meteorObject', ['$rootScope', '$meteorUtils', 'Ang
121138
var data = new AngularMeteorObject(collection, id, options);
122139

123140
data.autorunComputation = $meteorUtils.autorun($rootScope, function() {
124-
data.reset();
141+
data.reset(true);
125142
});
126143

127144
if (auto) { // Deep watches the model and performs autobind.

package.js

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Package.onTest(function(api) {
7272
'tests/integration/angular-meteor-session-spec.js',
7373
'tests/integration/angular-meteor-diff-array-spec.js',
7474
'tests/integration/angular-meteor-collection-spec.js',
75+
'tests/integration/angular-meteor-object-spec.js',
7576
'tests/integration/angular-meteor-reactive-scope-spec.js',
7677
'tests/integration/angular-meteor-utils-spec.js',
7778
'tests/integration/test_collections.js'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
describe('$meteorObject service', function () {
2+
var $meteorObject,
3+
TestCollection
4+
id,
5+
meteorObject;
6+
7+
beforeEach(angular.mock.module('angular-meteor'));
8+
9+
beforeEach(angular.mock.inject(function (_$meteorObject_) {
10+
$meteorObject = _$meteorObject_;
11+
}));
12+
13+
beforeEach(function () {
14+
id = 'test_1'
15+
var data = { _id: id, a: 1, b: 2 };
16+
17+
TestCollection = new Mongo.Collection(null);
18+
TestCollection.insert(data);
19+
20+
meteorObject = $meteorObject(TestCollection, id, false);
21+
});
22+
23+
it('should delete the property when unset in the collection', function () {
24+
TestCollection.update(id, { $unset: { a: 1 } });
25+
26+
Tracker.flush();
27+
28+
var doc = TestCollection.findOne(id);
29+
expect(doc.a).not.toBeDefined('document have unset property'); // make sure it was unset in mongo
30+
31+
expect(meteorObject.a).not.toBeDefined('angular meteor object have unset property');
32+
});
33+
34+
35+
it('should keep the client only property after autorun updates', function () {
36+
meteorObject.clientProp = 'keep';
37+
38+
TestCollection.update(id, { $unset: { a: 1 } });
39+
40+
Tracker.flush();
41+
42+
var doc = TestCollection.findOne(id);
43+
expect(doc.clientProp).not.toBeDefined('document have client property'); // make sure it didn't set to mongo
44+
45+
expect(meteorObject.a).not.toBeDefined('angular meteor object have unset property');
46+
expect(meteorObject.clientProp).toBeDefined('angular meteor object doesnt have client property');
47+
});
48+
49+
it('should delete server and client side properties when object removed from the collection', function () {
50+
meteorObject.clientProp = 'keep'
51+
TestCollection.remove(id);
52+
53+
Tracker.flush();
54+
55+
expect(meteorObject.a).not.toBeDefined();
56+
expect(meteorObject.b).not.toBeDefined();
57+
expect(meteorObject._id).not.toBeDefined();
58+
expect(meteorObject.clientProp).not.toBeDefined();
59+
});
60+
61+
describe('#reset()', function () {
62+
63+
it('should remove the client property', function () {
64+
meteorObject.clientProp = 'delete';
65+
66+
meteorObject.reset();
67+
68+
expect(meteorObject.clientProp).not.toBeDefined('angular meteor object doesnt have client property');
69+
});
70+
});
71+
});

0 commit comments

Comments
 (0)