<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.min.js"></script>
<script src="https://cdn.firebase.com/js/client/2.2.4/firebase.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-data/2.8.2/js-data.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-data-firebase/2.1.1/js-data-firebase.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-data-angular/3.1.0/js-data-angular.js"></script>
<script src="script.js"></script>
</head>
<body>
<h1>Open up your console to see the output</h1>
</body>
</html>
angular.module('app', ['js-data'])
.config(function(DSFirebaseAdapterProvider) {
// This is so I can load the fake data from plunker
DSFirebaseAdapterProvider.defaults.basePath = 'https://js-data-firebase.firebaseio.com';
})
.service('User', function(DS) {
return DS.defineResource({
name: 'user',
relations: {
hasMany: {
post: {
// localField is for linking relations
localField: 'posts', // user.posts -> array of posts of this user
// foreignKey is the "join" field
foreignKey: 'userId' // the name of the field on a post that points to its parent user
},
comment: {
// localField is for linking relations
localField: 'comments', // user.comments -> array of comments of this user
// foreignKey is the "join" field
foreignKey: 'userId' // the name of the field on a comment that points to its parent user
}
}
}
});
})
.service('Post', function(DS) {
return DS.defineResource({
name: 'post',
relations: {
hasMany: {
comment: {
// localField is for linking relations
localField: 'comments', // concept.comments -> array of comments of this post
// foreignKey is the "join" field
foreignKey: 'postId' // the name of the field on a comment that points to its parent post
}
},
belongsTo: {
user: {
// localField is for linking relations
localField: 'user', // post.user -> reference to this post's user
// localKey is the "join" field
localKey: 'userId' // the name of the field on an post that points to its parent user
}
}
}
});
})
.service('Comment', function(DS) {
return DS.defineResource({
name: 'comment',
relations: {
belongsTo: {
post: {
// localField is for linking relations
localField: 'post', // comment.post -> reference to this comment's post
// localKey is the "join" field
localKey: 'postId' // the name of the field on a comment that points to its parent post
},
user: {
// localField is for linking relations
localField: 'user', // comment.user -> reference to this comment's user
// localKey is the "join" field
localKey: 'userId' // the name of the field on a comment that points to its parent user
}
}
}
});
})
.run(function(DS, DSFirebaseAdapter, User, Post, Comment) {
DS.registerAdapter('fb', DSFirebaseAdapter, { default: true });
return User.create({
name: 'John'
}).then(function (user) {
return Post.create({
title: 'foo',
userId: user.id
}).then(function (post) {
return Comment.create({
content: 'bar',
postId: post.id,
userId: user.id
});
});
}).then(function (comment) {
console.log(comment);
console.log(comment.user);
console.log(comment.post);
console.log(User.get(comment.userId));
// keep my firebase clean
return User.destroyAll();
}).then(function () {
return Post.destroyAll();
}).then(function () {
return Comment.destroyAll();
});
});