EmberData: Two models related with hasMany relationships

后端 未结 2 1725
滥情空心
滥情空心 2021-02-03 15:55

I have an application logic that requires two models to have reciprocal hasMany relationships. As an example, imagine a set of GitHub issues that can be tagged with several labe

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-03 16:23

    We use a similar method of creating the association object. However, instead of overriding the methods in store, we just added the join objects to the api.

    so in the models we create:

    App.Hashtag = DS.Model.extend({
      hashtagUsers: DS.hasMany('App.HashtagUser', {key: 'hashtag_user_ids'})   
    });
    
    App.User = DS.Model.extend({
      hashtagUsers: DS.hasMany('App.HashtagUser', {key: 'hashtag_user_ids'})
    });
    
    App.HashtagUser = DS.Model.extend({
      user: DS.belongsTo('App.User'),
      hashtag: DS.belongsTo('App.Hashtag')
    });
    

    Then for the transactions we simply alter and commit the join object.

    App.UserController = Ember.ObjectController.extend({
      followHashtag: function(tag) {
        var hashtagUser;
        hashtagUser = this.get('hashtagUsers').createRecord({
          hashtag: tag
        });
        tag.get('hashtagUsers').pushObject(hashtagUser);
        App.store.commit();
      }
      unfollowHashtag: function(tag) {
        var itemToRemove;
        itemToRemove = this.get('hashtagUsers').find(function(hashtagUser) {
          if (hashtagUser.get('hashtag') === this) {
            return true;
          }
        }, tag);
        this.get('hashtagUser').removeObject(itemToRemove);
        tag.get('hashtagUser').removeObject(itemToRemove);
        itemToRemove.deleteRecord();
        App.store.commit();   
    

    });

    The API creates a HashtagUser object and the follow method just adds that user to both the associated pieces.

    For removal, it pops the associated objects and destroys the association object.

    Although it's not as elegant as it could be, our big motivation was that when Ember Data gets updated then we should be able to transition it to a simple stock Ember Data supported version more easily than if we've messed with the Store itself.

提交回复
热议问题