Given the following models:
var post = DS.Model.extend({
tags: DS.hasMany('tag', {async: true, inverse: 'posts'})
});
var tag = DS.Model.extend({
posts: DS.hasMany('post', {async: true, inverse: 'tags'})
});
Adding or removing a tag looks like this:
var post = this.get('model');
this.store.createRecord('tag', {}).save().then(function (tag) {
post.get('tags').then(function (tags) {
tags.addObject(tag);
post.save().then(function () {
tag.get('posts').then(function (posts) {
posts.addObject(post);
tag.save().catch(function (res) {
// handle errors
});
});
}).catch(function (res) {
// handle errors
});
});
});
I feel like there must be a better way of handling the thenables - but I am still trying to wrap my head around promises and how to use them properly.
So, what is the best way to add or remove a tag from a particular post?
Aucun commentaire:
Enregistrer un commentaire