We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The issue
I often mirror collections and I need collection helpers to be transferred from one collection to the other.
The solution that comes immediately to mind is:
MyCollectionB = new Mongo.Collection('myCollectionB', { transform: MyCollectionA._transform });
But this solution fails in the following use case:
MyCollectionA.helpers({ next: function() { return MyCollectionA.findOne({ order: { $gt: this.order } }, { sort: { order: 1 } }); } });
Indeed, if I transfer this helper to MyCollectionB using the above solution, I get MyCollectionB::next() returning an item from MyCollectionA!
MyCollectionB
MyCollectionB::next()
MyCollectionA
What I did to solve the issue
In collection-helpers.js, I have added the following line at the very end:
collection-helpers.js
self._helpers.prototype.collection = function() { return self; };
Then I do this:
SharedHelpers = { next: function() { return this.collection().findOne({ order: { $gt: this.order } }, { sort: { order: 1} }); } }; MyCollectionA.helpers(SharedHelpers); MyCollectionB.helpers(SharedHelpers);
Any advice on how to improve this?
The text was updated successfully, but these errors were encountered:
Hey @steph643 I've decided it's best not to provide an API for this as there's many simple methods you can use to achieve this, here's one example:
_.each([Books, Authors], function(collection) { collection.helpers({ formattedCreatedAt: function() { return moment(this.createdAt).format('MMMM Do YYYY, h:mm a'); }, formattedUpdatedAt: function() { return moment(this.updatedAt).format('MMMM Do YYYY, h:mm a'); } }); });
There was a bit of discussion on it early on, see here: #3
Sorry, something went wrong.
Much cleaner, indeed.
For those landing here, here is what I have finally done, to allow for collectionA to be client/server and collectionB to be client only:
SharedHelpers = function(collection) { return { next: function() { return collection.findOne({ order: { $gt: this.order } }, { sort: { order: 1} }); } } }; // File 1 MyCollectionA.helpers(SharedHelpers(MyCollectionA)); // File 2 MyCollectionB.helpers(SharedHelpers(MyCollectionB));
No branches or pull requests
The issue
I often mirror collections and I need collection helpers to be transferred from one collection to the other.
The solution that comes immediately to mind is:
But this solution fails in the following use case:
Indeed, if I transfer this helper to
MyCollectionB
using the above solution, I getMyCollectionB::next()
returning an item fromMyCollectionA
!What I did to solve the issue
In
collection-helpers.js
, I have added the following line at the very end:Then I do this:
Any advice on how to improve this?
The text was updated successfully, but these errors were encountered: