Skip to content

Instantly share code, notes, and snippets.

Created July 8, 2015 06:20
Show Gist options
  • Save anonymous/28b94f965f3244d1f5ef to your computer and use it in GitHub Desktop.
Save anonymous/28b94f965f3244d1f5ef to your computer and use it in GitHub Desktop.
Many to Many Relationships Blog and Author (Working Solution) // source http://jsbin.com/facuno
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- Ember + Ember Data -->
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://builds.emberjs.com/tags/v1.11.3/ember-template-compiler.js"></script>
<script src="http://builds.emberjs.com/tags/v1.11.3/ember.debug.js"></script>
<script src="http://builds.emberjs.com/tags/v1.0.0-beta.12/ember-data.min.js"></script>
<!-- Firebase -->
<script src="https://cdn.firebase.com/js/client/2.2.7/firebase.js"></script>
<!-- EmberFire -->
<script src="https://cdn.firebase.com/libs/emberfire/1.4.5/emberfire.min.js"></script>
<title> Many to Many Relationships Blog and Author (Working Solution)</title>
</head>
<body>
Many to Many Relationships Blog and Author (Working Solution)<br>
<script type="text/x-handlebars">
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
<button {{action 'savePost'}}> Save Post </button><br>
Posts <br>
{{#each model.posts as |record|}}
{{record.id}} - {{record.msg}} - {{record.authors.length}}<br>
{{/each}}<br>
Users<br>
{{#each model.users as |record|}}
{{record.id}} - {{record.name}} - {{record.posts.length}}<br>
{{/each}}<br>
</script>
<script id="jsbin-javascript">
App = Ember.Application.create();
App.ApplicationAdapter = DS.FirebaseAdapter.extend({
firebase: new Firebase('https://many.firebaseio.com/')
});
App.ApplicationSerializer = DS.FirebaseSerializer.extend();
App.Router.map(function(){ });
App.Post = DS.Model.extend({
msg: DS.attr(),
authors: DS.hasMany('user', { async: true }),
});
App.User = DS.Model.extend({
name: DS.attr(),
posts: DS.hasMany('post', { async: true })
});
App.IndexRoute = Ember.Route.extend({
model: function() {
var posts = this.store.find('post');
var users = this.store.find('user');
return {
posts: posts,
users: users
};
},
actions: {
savePost: function(){
var post = this.store.createRecord('post',{msg: 'round the rugged rock'});
var author1 = this.store.createRecord('user', {name: 'john'});
var author2 = this.store.createRecord('user', {name: 'jane'});
post.save()
.then(function(){
return Ember.RSVP.Promise.all([
author1.save(),
author2.save()
]);
})
.then(function(){
var promises = [];
console.log('post saved', post.id, 'author1', author1.id, 'author2', author2.id);
promises.push(post.get('authors'));
promises.push(author1.get('posts'));
promises.push(author2.get('posts'));
return Ember.RSVP.Promise.all(promises);
})
.then(function(arrayOfAuthorsPosts1Posts2){
var relAuthors = arrayOfAuthorsPosts1Posts2[0],
relPosts1 = arrayOfAuthorsPosts1Posts2[1],
relPosts2 = arrayOfAuthorsPosts1Posts2[2],
promises = [];
relAuthors.addObjects(author1, author2);
relPosts1.addObject(post);
relPosts2.addObject(post);
console.log('relationships added');
promises.addObjects(post.save(), author1.save(), author2.save());
return Ember.RSVP.Promise.all(promises);
})
.then(function() {
//everything is done!
console.log('saved all records, post authors:', post.get('authors').toArray());
});
}
}
});
</script>
<script id="jsbin-source-javascript" type="text/javascript">
App = Ember.Application.create();
App.ApplicationAdapter = DS.FirebaseAdapter.extend({
firebase: new Firebase('https://many.firebaseio.com/')
});
App.ApplicationSerializer = DS.FirebaseSerializer.extend();
App.Router.map(function(){ });
App.Post = DS.Model.extend({
msg: DS.attr(),
authors: DS.hasMany('user', { async: true }),
});
App.User = DS.Model.extend({
name: DS.attr(),
posts: DS.hasMany('post', { async: true })
});
App.IndexRoute = Ember.Route.extend({
model: function() {
var posts = this.store.find('post');
var users = this.store.find('user');
return {
posts: posts,
users: users
};
},
actions: {
savePost: function(){
var post = this.store.createRecord('post',{msg: 'round the rugged rock'});
var author1 = this.store.createRecord('user', {name: 'john'});
var author2 = this.store.createRecord('user', {name: 'jane'});
post.save()
.then(function(){
return Ember.RSVP.Promise.all([
author1.save(),
author2.save()
]);
})
.then(function(){
var promises = [];
console.log('post saved', post.id, 'author1', author1.id, 'author2', author2.id);
promises.push(post.get('authors'));
promises.push(author1.get('posts'));
promises.push(author2.get('posts'));
return Ember.RSVP.Promise.all(promises);
})
.then(function(arrayOfAuthorsPosts1Posts2){
var relAuthors = arrayOfAuthorsPosts1Posts2[0],
relPosts1 = arrayOfAuthorsPosts1Posts2[1],
relPosts2 = arrayOfAuthorsPosts1Posts2[2],
promises = [];
relAuthors.addObjects(author1, author2);
relPosts1.addObject(post);
relPosts2.addObject(post);
console.log('relationships added');
promises.addObjects(post.save(), author1.save(), author2.save());
return Ember.RSVP.Promise.all(promises);
})
.then(function() {
//everything is done!
console.log('saved all records, post authors:', post.get('authors').toArray());
});
}
}
});
</script></body>
</html>
App = Ember.Application.create();
App.ApplicationAdapter = DS.FirebaseAdapter.extend({
firebase: new Firebase('https://many.firebaseio.com/')
});
App.ApplicationSerializer = DS.FirebaseSerializer.extend();
App.Router.map(function(){ });
App.Post = DS.Model.extend({
msg: DS.attr(),
authors: DS.hasMany('user', { async: true }),
});
App.User = DS.Model.extend({
name: DS.attr(),
posts: DS.hasMany('post', { async: true })
});
App.IndexRoute = Ember.Route.extend({
model: function() {
var posts = this.store.find('post');
var users = this.store.find('user');
return {
posts: posts,
users: users
};
},
actions: {
savePost: function(){
var post = this.store.createRecord('post',{msg: 'round the rugged rock'});
var author1 = this.store.createRecord('user', {name: 'john'});
var author2 = this.store.createRecord('user', {name: 'jane'});
post.save()
.then(function(){
return Ember.RSVP.Promise.all([
author1.save(),
author2.save()
]);
})
.then(function(){
var promises = [];
console.log('post saved', post.id, 'author1', author1.id, 'author2', author2.id);
promises.push(post.get('authors'));
promises.push(author1.get('posts'));
promises.push(author2.get('posts'));
return Ember.RSVP.Promise.all(promises);
})
.then(function(arrayOfAuthorsPosts1Posts2){
var relAuthors = arrayOfAuthorsPosts1Posts2[0],
relPosts1 = arrayOfAuthorsPosts1Posts2[1],
relPosts2 = arrayOfAuthorsPosts1Posts2[2],
promises = [];
relAuthors.addObjects(author1, author2);
relPosts1.addObject(post);
relPosts2.addObject(post);
console.log('relationships added');
promises.addObjects(post.save(), author1.save(), author2.save());
return Ember.RSVP.Promise.all(promises);
})
.then(function() {
//everything is done!
console.log('saved all records, post authors:', post.get('authors').toArray());
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment