ground-db icon indicating copy to clipboard operation
ground-db copied to clipboard

How to get offline method callback?

Open satyavh opened this issue 9 years ago • 7 comments
trafficstars

consider this example:

    Meteor.call 'doSomething', (err, suc) =>
      if err
        console.log 'error!'
      else
        console.log 'succes!'

When online this works fine. But when offline, there's never a callback. How to get success from Ground for a method call?

satyavh avatar Dec 04 '15 08:12 satyavh

events in ground db is poorly documented - but you can do Ground.on('method', function(evt) {});: https://github.com/GroundMeteor/db/blob/connection-driver/groundDB.client.js#L728

raix avatar Dec 04 '15 08:12 raix

Thanks for your quick response. However this event only seems to be emitted after the connection becoming online again.

I need a callback while being offline.

satyavh avatar Dec 04 '15 09:12 satyavh

so you want a callback when you do the method call? Meteor.call returns the callback when the server has responded.

raix avatar Dec 04 '15 09:12 raix

I understand.

But my code is waiting for a callback to continue, so it breaks the code.

With Ground, when offline, method calls are being saved to be resumed later. So there's no callback -obviously - from server. But it would be great if Ground gives a callback instead that the method is successfully saved (perhaps via an event).

For example, if you're method is called 'doSomething' add an event handler:

Ground.on 'doSomething', (status) ->
  console.log status
  # status = saved / not saved

satyavh avatar Dec 04 '15 10:12 satyavh

got it

raix avatar Dec 04 '15 11:12 raix

@satyavh I know it's not the best solution but currently for this case I am using caolan/async - here's an example: I want to update 2 records from different Collections and have the callbacks running even in offline mode, I do somethinig like this.

async.parallel [
  (callback) ->
    a = Collection.update
      _id: some._id
    ,
      $set:
        value: "some value"
    , (error) ->
      callback(error, "Collection")

    # even if offline
    if a and !Meteor.status().connected then callback(null, "Collection")

  (callback) ->
    b = Collection2.update
      _id: some._id
    ,
      $set:
        value: "some other value"
    , (error) ->
      callback(error, "Collection")

    # even if offline
    if b and !Meteor.status().connected then callback(null, "Collection2")  

], (err, results) ->
  if err
    console.log err.message
  else
    # success
    # do something when both are successfull :)

Hope this helps.

Todor

tagrudev avatar Dec 08 '15 07:12 tagrudev

Hello tagrudev, what's the meteor package for this?

vinodb2806 avatar Feb 12 '16 09:02 vinodb2806