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

Collection Helpers #180

Open
KiwiKilian opened this issue Aug 27, 2016 · 3 comments
Open

Collection Helpers #180

KiwiKilian opened this issue Aug 27, 2016 · 3 comments

Comments

@KiwiKilian
Copy link

I'm completely new to ground:db and grounded succesfully my database in a cordova app. The app only needs to read, no operations to the database are needed. But I'm using a lot of Collection Helpers (dburles:collection-helpers ) which can't be accessed when grounded (says they are not defined), because the functions aren't attached to the object returned when taken frome the grounded collection. Also I can't attach Collection Helpers to Ground.Collection (says GroundedCollection.helpers is not defined).

export const Artworks = new Mongo.Collection('artworks');

if(Meteor.isCordova){
    const  ArtworksGround = new Ground.Collection(' ArtworksGround');

    ArtworksGround.observeSource(Artworks.find());

    Meteor.subscribe('artworks.all');

    Artworks.find = function(...args) {
        return  ArtworksGround.find(...args);
    };

    Artworks.findOne = function(...args) {
        return  ArtworksGround.findOne(...args);
    };
}

Artworks.helpers({
    helperFunction() {
        // do something...
        return something;
        }
});

I know this problem is connected with the other package but I thought it would be likelier somebody around here knows a workaround for this.

@richardgsands
Copy link

richardgsands commented Aug 19, 2017

I'm trying to figure out a way to do this too. Tried adding the helpers function to Ground.Collection.prototype, and then setting Mongo.Collection helpers on there, but it doesn't seem to work... probably some sneaky scoping issue!! Any assistance would be very much appreciated :)

// dburles:collection-helpers for Ground.Collection
// https://github.com/dburles/meteor-collection-helpers/blob/master/collection-helpers.js
Ground.Collection.prototype.helpers = function(helpers) {
    var self = this;
  
    if (self._transform && ! self._helpers)
      throw new Meteor.Error("Can't apply helpers to '" +
        self._name + "' a transform function already exists!");
  
    if (! self._helpers) {
      self._helpers = function Document(doc) { return _.extend(this, doc); };
      self._transform = function(doc) {
        return new self._helpers(doc);
      };
    }
  
    _.each(helpers, function(helper, key) {
      self._helpers.prototype[key] = helper;
    });
};

function _ground(collection, selector) {

    // new references to find and findOne
    collection.findOnline = collection.find;
    collection.findOneOnline = collection.findOne;

    // setup ground collection
    collection.groundCollection = new Ground.Collection(`_${collection._name}`);
    collection.groundCollection.observeSource(collection.find(selector || {}));

    // override find and findOne with ground collection functions
    collection.find = collection.groundCollection.find;
    collection.findOne = collection.groundCollection.findOne;

    // add helpers
    if (collection._helpers.prototype)
         collection.groundCollection.helpers(collection._helpers.prototype);

}

_ground(MyCollection);

The result of that is the MyCollection.findOneOnline().myHelper() works but MyCollection.findOne().myHelper() throws an error:

TypeError: MyCollection.findOne().myHelper is not a function. (In 'MyCollection.findOne().myHelper()', 'MyCollection.findOne().myHelper' is undefined)

@richardgsands
Copy link

Update: MyCollection.findOneOnline (i.e. the normal ungrounded version) returns a Document, whereas MyCollection.findOne (the grounded collection find method) returns an Object... watch this space!

@ashish979
Copy link

@richardgsands I am facing the same problem. Are you able to solve this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants