Skip to content

Commit d8fb9ab

Browse files
committed
Merge commit '198125c37962b10462e311440d46767a7c41d782'
2 parents 17f1d82 + 198125c commit d8fb9ab

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

README.markdown

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,29 @@ ddpclient.connect(function(error) {
6666
}, 3000);
6767

6868
/*
69-
* Subscribe to a Meteor Xollection
69+
* Call a Meteor Method while passing in a random seed.
70+
* Added in DDP pre2, the random seed will be used on the server to generate
71+
* repeatable IDs. This allows the same id to be generated on the client and server
72+
*/
73+
var Random = require("ddp-random"),
74+
random = Random.createWithSeeds("randomSeed"); // seed an id generator
75+
76+
ddpclient.callWithRandomSeed(
77+
'createPost', // name of Meteor Method being called
78+
[{ _id : random.id(), // generate the id on the client
79+
body : "asdf" }],
80+
"randomSeed", // pass the same seed to the server
81+
function (err, result) { // callback which returns the method call results
82+
console.log('called function, result: ' + result);
83+
},
84+
function () { // callback which fires when server has finished
85+
console.log('updated'); // sending any updated documents as a result of
86+
console.log(ddpclient.collections.posts); // calling this method
87+
}
88+
);
89+
90+
/*
91+
* Subscribe to a Meteor Collection
7092
*/
7193
ddpclient.subscribe(
7294
'posts', // name of Meteor Publish function to subscribe to

examples/example.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,29 @@ ddpclient.connect(function(error) {
4646
}, 3000);
4747

4848
/*
49-
* Subscribe to a Meteor Xollection
49+
* Call a Meteor Method while passing in a random seed.
50+
* Added in DDP pre2, the random seed will be used on the server to generate
51+
* repeatable IDs. This allows the same id to be generated on the client and server
52+
*/
53+
var Random = require("ddp-random"),
54+
random = Random.createWithSeeds("randomSeed"); // seed an id generator
55+
56+
ddpclient.callWithRandomSeed(
57+
'createPost', // name of Meteor Method being called
58+
[{ _id : random.id(), // generate the id on the client
59+
body : "asdf" }],
60+
"randomSeed", // pass the same seed to the server
61+
function (err, result) { // callback which returns the method call results
62+
console.log('called function, result: ' + result);
63+
},
64+
function () { // callback which fires when server has finished
65+
console.log('updated'); // sending any updated documents as a result of
66+
console.log(ddpclient.collections.posts); // calling this method
67+
}
68+
);
69+
70+
/*
71+
* Subscribe to a Meteor Collection
5072
*/
5173
ddpclient.subscribe(
5274
'posts', // name of Meteor Publish function to subscribe to

lib/ddp-client.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,19 @@ DDPClient.prototype.call = function(name, params, callback, updatedCallback) {
337337
self._send({msg: 'method', id: id, method: name, params: params});
338338
};
339339

340+
DDPClient.prototype.callWithRandomSeed = function(name, params, randomSeed, callback, updatedCallback) {
341+
var self = this;
342+
var id = self._nextId();
343+
344+
if (callback)
345+
self._callbacks[id] = callback;
346+
347+
if (updatedCallback)
348+
self._updatedCallbacks[id] = updatedCallback;
349+
350+
self._send({msg: 'method', id: id, method: name, randomSeed: randomSeed, params: params});
351+
};
352+
340353
// open a subscription on the server, callback should handle on ready and nosub
341354
DDPClient.prototype.subscribe = function(name, params, callback) {
342355
var self = this;

0 commit comments

Comments
 (0)