diff --git a/README.md b/README.md index eef5c72..2d59d82 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,18 @@ Unsubscribes to a server publication. - `id` **string** *required* : id of the server publication +### on(eventName, collectionName, callback) + +Subscribe to DDP events. + +#### Arguments + +- `eventName` **string** *required* : Name of the event (`connected`, `disconnected`, `added`, `changed`, `removed`) + +- `collectionName` **string** *optional* : collection name to specify when event name = added, changed or removed. + +- `callback` **function** *required* : callback called when event is emitted. Returns : element (added), element (changed), id (removed) + ### itemSubscribe(name, collectionName, id, callback) Subscribes to an item in a collection (the collection need to be subscribed with same name and collection name parameter). Returns the subscriptionId. @@ -124,16 +136,6 @@ Unsubscribes to a item subscription. - `subId` **string** *required* : id of the subscription -### on(eventName, callback) - -Callback when an event is triggered - -#### Arguments - -- `eventName` **string** *required* : 'connected' and 'disconnected' only for the moment - -- `callback` **function** *required* - ### method(name, [args], callback) @@ -178,7 +180,3 @@ Login to meteor server via a token ### logout(callback) Logout from meteor server - -#### Warning - -You can only do one subscription on a same collection at one time diff --git a/index.js b/index.js index 915eaca..1a283b2 100644 --- a/index.js +++ b/index.js @@ -205,6 +205,9 @@ module.exports = { ddp.on("added", function (message) { subscriptions = subscriptions.map(function (sub) { if(sub.collectionName == message.collection) { + + queue.emit('added', sub.collectionName, message.fields); + message.fields.id = message.id; sub.items.push(message.fields); if(sub.ready) { @@ -236,6 +239,9 @@ module.exports = { ddp.on("removed", function (message) { subscriptions = subscriptions.map(function (sub) { if(sub.collectionName == message.collection && !sub.removed) { + + queue.emit('removed', sub.collectionName, message.id); + sub.items = sub.items.filter(function (item) { if(item.id == message.id) return false; return true; @@ -249,6 +255,11 @@ module.exports = { ddp.on("changed", function (message) { subscriptions = subscriptions.map(function (sub) { if(sub.collectionName == message.collection) { + + var item = Object.assign({}, message.fields); + item.id = message.id; + queue.emit('changed', sub.collectionName, item); + sub.items = sub.items.map(function (item) { if(item.id==message.id) { var res = { diff --git a/package.json b/package.json index f3d9fed..f19b947 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-meteor", - "version": "0.5.1", + "version": "0.6.0", "description": "DDP React-native Client", "main": "index.js", "scripts": { diff --git a/queue.js b/queue.js index e3c60f4..da65fb4 100644 --- a/queue.js +++ b/queue.js @@ -3,15 +3,25 @@ var callbacks = []; module.exports = { - on: function (eventName, callback) { + on: function (eventName, collectionName, callback) { + if(callback === undefined) { + callback = collectionName; + collectionName = ''; + } callbacks.push({ eventName: eventName, + collectionName: collectionName, callback: callback }); }, - emit: function (eventName, message) { + emit: function (eventName, collectionName, message) { + if(message === undefined) { + message = collectionName; + collectionName = ''; + } + var eventCallbacks = callbacks.filter(function (callback) { - if(callback.eventName == eventName) return true; + if(callback.eventName == eventName && callback.collectionName == collectionName) return true; return false; }).map(function (callback) { return callback.callback;