Skip to content
New issue

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

Making helpers available to another collection? #36

Closed
steph643 opened this issue Jun 2, 2015 · 2 comments
Closed

Making helpers available to another collection? #36

steph643 opened this issue Jun 2, 2015 · 2 comments

Comments

@steph643
Copy link

steph643 commented Jun 2, 2015

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!

What I did to solve the issue

In collection-helpers.js, I have added the following line at the very end:

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?

@steph643 steph643 changed the title Make helpers available to another collection? Making helpers available to another collection? Jun 2, 2015
@dburles
Copy link
Owner

dburles commented Jun 3, 2015

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

@steph643
Copy link
Author

steph643 commented Jun 3, 2015

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));

@dburles dburles closed this as completed Jun 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants