如何为模型添加/删除hasMany关系?

时间:2014-05-30 18:36:11

标签: ember.js ember-data

所以我想说我有两种模式:用户预订

用户

var User = DS.Model.extend({
    name: DS.attr('string'),
    books: DS.hasMany('book')
});

图书

var Book = DS.Model.extend({
    title: DS.attr('string'),
    users: DS.hasMany('user')
})

所以我希望能够“结账”并为某个用户归还书籍。鉴于以下 BookController ,最新的做法是删除和添加关系而不实际删除该书?

var BookController = Ember.ObjectController.extend({
    needs: ['userProfile'],
    actions: {
        checkOutBook: function(){
            var currentBook = this.get('model');
            var userBooks = this.get('controllers.userProfile.model.books');
            //add relationship to user
        },
        returnBook: function() {
            var currentBook = this.get('model');
            var userBooks = this.get('controllers.userProfile.model.books');
            //remove relationship to user
        }
    }
})

使用Ember 1.5.1Ember-Data 1.0.0-beta.8.2a68c63a

1 个答案:

答案 0 :(得分:0)

//add
userBooks.pushObject(currentBook);

//remove
userBooks.removeObject(currentBook)