Skip to content

stfnbrgh/meteor-nested-collection-helpers

 
 

Repository files navigation

Based on dburles:collection-helpers but adds support for nesting helpers on embedded documents. If you do not require nesting see https://atmospherejs.com/dburles/collection-helpers and https://github.com/dburles/meteor-collection-helpers. This package might become an add-on for collection-helpers in the future.

TODO:

  • Tests need to be updated

Meteor Collection Helpers

Collection helpers automatically sets up a transformation on your collections using Meteor's Mongo.Collection transform option, allowing for simple models, with an interface similar to template helpers.

Installation

There is not currently a package with this functionality. Just clone into your packages folder and do

$ meteor add stfnbrgh:nested-collection-helpers

This package is not currently compatible with dburles:collection-helpers and is offered as an alternative. It is also quite new so stuff like the fact that it still has the same name etc. will be sorted out as time allows.

Usage - TODO: Better examples

It's recommended to set up helpers to run on both server and client. This way your helpers can be accessed both server side and client side.

###Pre-requisites Define some collections.

Books = new Mongo.Collection('books');
Authors = new Mongo.Collection('authors');

// A book document looks like:
// {
//     name: "A book written for me",
// 	   authorId: "xsdafdawd...",
// 	   publisher: "Pubs united",
// 	   foreword: {
// 		   author: "Foreword Author"
// 		   title: "Foreword"
// 		   paragraphs: [ ... ] /*strings*/,		
// 	   },
// 	   chapters: [
//	       { title: "...", pages: [ ... ] /*strings*/ }
//	   ]
// }

###Basic Helpers Attaches helpers to the root document(s) returned from a collection.

Books.helpers({
  author: function() {
    return Authors.findOne(this.authorId);
  },
  remove: function(){
	Books.remove({_id: this._id});
  }
});
// bookInstance.remove() // remove this book from the Books collection
// bookInstance.author().firstName 

###Nested Helpers Attaches helpers to the embeded document(s) returned from a collection.

Books.helpers({
	"foreword.numberOfParagraphs": function(helperContext){
		//helperContext -> { parentDocument: { ... }, rootDocument: { ... } }
		//this -> { author: "...", title: "...", paragraphs: [...] }	
		
		return this.paragraphs ? this.paragraphs.length : 0;
	}
});
// var n = bookInstance.foreword.numberOfParagraphs() // number of paragraphs in foreword

###Array Nested Helpers Attaches helpers to the embeded document(s) inside arrays, returned from a collection.

Books.helpers({
	"chapters.$.numberOfPages": function(helperContext){
		//helperContext -> { parentDocument: { ... }, rootDocument: { ... } }
		//this -> { title: "...", pages: [ ... ] }	
		
		return this.pages ? this.pages.length : 0;
	}
});
// bookInstance.chapters[2].numberOfPages(); // number of pages in chapter three
// bookInstance.chapters[5].numberOfPages(); // number of pages in chapter six
// p.s. You can nest after arrays as well: "some.array.$.elementField.array.$.very.deep.array.$.helperName": func ...

Example use within a template

Our relationships are resolved by the collection helper, avoiding unnecessary template helpers. So we can simply write:

Template.books.helpers({
  books: function() {
    return Books.find();
  }
});

...with the corresponding template:

// TODO: test this to make sure it works
<template name="books">
  <ul>
  {{#each books}}
    <li>
	<p>{{name}} by {{author.fullName}}</p>
	<ul>
		{{ #each chapters }}
		<li>{{ title }}: {{ numberOfPages }}</li>
		{{ /each }}
	</ul>	
	</li>
  {{/each}}
  </ul>
</template>

Meteor.users

You can also apply helpers to the Meteor.users collection

Meteor.users.helpers({
  // ...
});

Applying the transformation function

Sometimes it may be useful to apply the transformation directly to an object.

var doc = {
  firstName: 'Charles',
  lastName: 'Darwin'
};

transformedDoc = Authors._transform(doc);

transformedDoc.fullName(); // Charles Darwin

License

MIT

About

Based on dburles:collection-helpers. Adds support for nesting helpers.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • JavaScript 100.0%