Refresh a document in Meteor mini-mongo without updating server collections

In Meteor, I have a collection that the client signs. In some cases, instead of publishing documents that exist in the collection on the server, I want to send some dummy data. Now this is fine using the this.added function in the publication.

My problem is that I want to treat a dummy document as if it were a real document, especially it becomes a problem when I want to update it. For real documents, I run RealDocs.update , but when I perform this action in a dummy document, it fails because there is no representation on the server (and I would like to save it that way).

The collection API, which allowed me to pass something like local = true , would be fantastic, but I have no idea how difficult it is to implement it, and I don't want to change the main code.

Right now I am stuck in creating BogusDocs = new Meteor.Collection(null) , but this makes it difficult to populate the collection, since I have to either use hardcodes in the client code or use a method to get data from the server, and I have to call BogusDocs.update instead of RealDocs.update as soon as I communicate with dummy data.

Perhaps I could really paste the data into the server and make sure it was deleted later, but the data really has nothing to do with the server-side collection, so I would rather avoid this.

Any thoughts on how to approach this problem?

+7
meteor
source share
5 answers

After some further research (the site triggered by the events ), it turns out that you can change the local collection without making calls to the server. This is done by running the same methods as you, but on MyCollection._collection , and not just on Collection. MyCollection.update() will thus become MyCollection._collection.update() . Thus, using a simple shell, you can pass the usual arguments to the update call to update the collection as usual (which will try to call the server, which in turn will call your allow / deny rules), or we can add 'local' as the last argument only for updating in the client collection. Something like this should do it.

 DocsUpdateWrapper = function() { var lastIndex = arguments.length -1; if (arguments[lastIndex] === 'local') { Docs._collection.update(arguments.slice(0, lastIndex); } else { Docs.update(arguments) } } 

(Of course, this could be expanded to DocsWrapper, which also allows you to insert and delete). (Did not try to perform this function, but it should serve as an example.)

The biggest advantage of this is that we can use the same calls to retrieve documents from the local collection, regardless of whether they are local or live on the server. By adding a simple logical document, we can keep track of which documents are only local and which are not (improved DocsWrapper can check this bool so that we can even omit passing the β€œlocal” argument.), So we know how to update them.

+17
source share

There are some people working on local storage in the browser https://github.com/awwx/meteor-browser-store Perhaps you can adapt some of your ideas to provide "fake" documents.

0
source share

I would use the transform function in the collection to create an object that knows what to do with itself (on the client). Give it the root update method (real / dummy) and then call .update, not the generic one.

You can put the code from this.added into the conversion process.

0
source share

You can also create a local minimongo collection. Paste on callback

 @FoundAgents = new Meteor.Collection(null, Agent.transformData ) FoundAgents.remove({}) Meteor.call 'Get_agentsCloseToOffer', me, ping, (err, data) -> if err console.log JSON.stringify err,null,2 else _.each data, (item) -> FoundAgents.insert item 
0
source share

Perhaps this is interesting for you, I created two examples with local Meteor Local Collections on meteorpad. The first panel shows an example with a simple reactive record set: Sample_Publish_to_Local-Collection . The second will use the .observe collection .observe to listen for data: Collection.observe () .

0
source share

All Articles