Last active
August 29, 2015 14:12
-
-
Save machwatt/7cd46d177e179de9a531 to your computer and use it in GitHub Desktop.
Meteor.js Solve relations between Collections via transform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<head> | |
<title>mongo-relation-test</title> | |
</head> | |
<body> | |
{{> root}} | |
</body> | |
<template name="root"> | |
<h1>Authors</h1> | |
{{#if loadingAuthors}} | |
<p>Loading</p> | |
{{else}} | |
<ul> | |
{{#each authors}} | |
{{> author}} | |
{{/each}} | |
</ul> | |
{{/if}} | |
<h1>Books</h1> | |
{{#if loadingBooks}} | |
<p>Loading</p> | |
{{else}} | |
<ul> | |
{{#each books}} | |
{{> book}} | |
{{/each}} | |
</ul> | |
{{/if}} | |
</template> | |
<template name="author"> | |
<li>{{name}} -- {{_id}}</li> | |
</template> | |
<template name="book"> | |
<li>{{name}} -- Author: {{author_name}}</li> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Books = new Mongo.Collection("books"); | |
Authors = new Mongo.Collection("authors"); | |
if (Meteor.isClient) { | |
/** Subscribe to check if connection is alive, needed for findOne() */ | |
var authorsHandle = Meteor.subscribe('authors'); | |
var booksHandle = Meteor.subscribe('books'); | |
Template.root.helpers({ | |
books: function () { | |
return Books.find({}, { | |
limit: 10, transform: function (book) { | |
var author = Authors.findOne({_id: book.author_id}); | |
book.author_name = author.name; | |
return book; | |
} | |
}); | |
}, | |
authors: function () { | |
return Authors.find({}); | |
}, | |
loadingAuthors: function () { | |
return !authorsHandle.ready(); | |
}, | |
loadingBooks: function () { | |
return !booksHandle.ready(); | |
} | |
}); | |
} | |
if (Meteor.isServer) { | |
/** Create random authors and books on startup and connect via "random" author id */ | |
Meteor.startup(function () { | |
if (Authors.find({}).count() <= 0) { | |
var i; | |
for (i = 0; i < 10; i++) { | |
Authors.insert({name: "Author " + i, _id: "author" + i}); | |
} | |
} | |
if (Books.find({}).count() <= 0) { | |
var i; | |
for (i = 0; i < 10000; i++) { | |
Books.insert({name: "book" + i, author_id: "author" + (Math.floor(Math.random() * 10) + 1)}); | |
} | |
} | |
}); | |
/** Use publish to check if db connection is ready*/ | |
Meteor.publish("authors", function () { | |
return Authors.find(); | |
}); | |
Meteor.publish("books", function () { | |
return Books.find(); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** As an alternative to Transform.js it is possible to insert the transform method into the whole Collection */ | |
Books = new Mongo.Collection("books", {transform: function (book) { | |
var author = Authors.findOne({_id: book.author_id}); | |
book.author_name = author.name; | |
return book; | |
}}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment