Skip to content

Commit 19f4e79

Browse files
committed
initial
0 parents  commit 19f4e79

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.build*

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Meteor Collection Helpers
2+
3+
Add helpers to your collections, just like templates!
4+
5+
### Usage
6+
7+
A basic example
8+
9+
```javascript
10+
Books = new Meteor.Collection('books');
11+
Authors = new Meteor.Collection('authors');
12+
13+
Books.helpers({
14+
author: function() {
15+
return Authors.findOne(this.authorId);
16+
}
17+
});
18+
19+
Authors.helpers({
20+
fullName: function() {
21+
return this.firstName + ' ' + this.lastName;
22+
},
23+
books: function() {
24+
return Books.find({ authorId: this._id });
25+
}
26+
});
27+
```
28+
29+
### Credits
30+
31+
Thanks to Mathieu Bouchard's work on [collection-hooks](https://github.com/matb33/meteor-collection-hooks) which assisted a great deal with extending Meteor.Collection.

collection-helpers.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var constructor = Meteor.Collection;
2+
3+
Meteor.Collection = function(name, options) {
4+
if (typeof options === 'undefined')
5+
options = {};
6+
options.transform = function(doc) { return new Document(doc); };
7+
return constructor.call(this, name, options);
8+
};
9+
10+
Document = function(doc) { return _.extend(this, doc); };
11+
12+
Meteor.Collection.prototype = Object.create(constructor.prototype);
13+
Meteor.Collection.prototype.helpers = function(helpers) {
14+
_.each(helpers, function(helper, key) {
15+
Document.prototype[key] = helper;
16+
});
17+
};
18+
19+
for (var func in constructor) {
20+
if (constructor.hasOwnProperty(func)) {
21+
Meteor.Collection[func] = constructor[func];
22+
}
23+
}

package.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Package.describe({
2+
summary: 'Collection helpers'
3+
});
4+
5+
Package.on_use(function(api) {
6+
api.add_files('collection-helpers.js', ['client', 'server']);
7+
});

smart.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "collection-helpers",
3+
"description": "Add helpers to your collections, just like templates.",
4+
"homepage": "https://github.com/dburles/meteor-collection-helpers",
5+
"author": "David Burles (http://twitter.com/dburles)",
6+
"version": "0.1.0",
7+
"git": "https://github.com/dburles/meteor-collection-helpers.git"
8+
}

0 commit comments

Comments
 (0)