How to insert data in ground.db in meteor when offline

616 views Asked by At

i have a fully working meteor application but now i want to make it offline so I installed ground:db and appcache here is my package file:

...
ground:db
appcache
ground:localstorage

then I changed my Collections to this:

Gebiete = new Mongo.Collection('gebiete');
Straßen = new Mongo.Collection('straßen');
Nummern = new Mongo.Collection('nummern');

Ground.Collection(Gebiete);
Ground.Collection(Straßen);
Ground.Collection(Nummern);

and now when the app is online i can insert data and then i disconnect the app and relaunch (cordova) and no data is lost.

But when im offline and i want to insert sth. it doesnt work ;(. I thaught i dont have to change my methods file but here is one method just to make sure if it is right:

Meteor.methods({
    neuesGebiet(Besitzer, Gebietsname, Gebietsnummer, Ort, Erstellungsdatum) {

         Gebiete.insert({ 
            Besitzer: Besitzer,         
            Gebietsname: Gebietsname,
            Gebietsnummer: Gebietsnummer,
            Ort: Ort,
            Erstellungsdatum: Erstellungsdatum
        });        
    }
});

and on the client the message is called like this: import { Meteor } from 'meteor/meteor'

Template.neuesGebietErstellen.onCreated(function () {
    this.subscribe('gebiete');
});


Template.neuesGebietErstellen.events({
    "submit .add-Gebiet": function (event) {
        var Gebietsname = event.target.Gebietsname.value;
        var Gebietsnummer = event.target.Gebietsnummer.value;
        var Ort = event.target.Ort.value;
        var Besitzer = Meteor.userId();
        var Erstellungsdatum = new Date();
        var Datum = Erstellungsdatum.toLocaleDateString();

        Meteor.call('neuesGebiet', Besitzer, Gebietsname, Gebietsnummer, Ort, Datum)

        FlowRouter.go('/');
        return false;        
    }
});

Please help me to get the data inserted when offline because i want it to be 100% offline

Thank you ;)

1

There are 1 answers

0
Sid Kwakkel On

It's been a while since I used Ground:db but here's what I think you are missing...

First, you probably only want grounded collections on Cordova, so

if(Meteor.isCordova) {
  Ground.Collection(Gebiete);
  Ground.Collection(Straßen);
  Ground.Collection(Nummern);
}

Then you need to use Groundmethods to store your method calls. So after the definition of the method:

Meteor.methods({
  'neuesGebiet': function(...) {
    ...
  }
});

if( Meteor.isClient ) {
  Ground.methodResume([
    'neuesGebiet'
  ]);
}