EmberData:与模型相关的两个模型 [英] EmberData: Two models related with hasMany relationships

查看:119
本文介绍了EmberData:与模型相关的两个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用逻辑,需要两个模型来互惠有许多关系。例如,想象一下可以使用多个标签来标记的一组GitHub问题。



我正在尝试使用扩展默认RESTAdapter的适配器。所有的应用程序工作正常,但双重有许多关系引发异常。挖掘代码,一个方法inverseBelongsToForHasMany引发异常。



所以,我猜Ember.Data不支持两个模型与两边的hasMany关系的关联,每个都需要一个关联的belongsTo。我的问题是:


  1. 这是否受支持,问题只是我做错了?

  2. 如果不支持,是否有计划出现的功能?

  3. 这种应用程序中是否避免使用这种关联类型?如果是,最好的方法或解决方法?

提前感谢

解决方案

我们使用类似的创建关联对象的方法。但是,在我们创建的模型中,我们只是将连接对象添加到api。



,而不是覆盖存储中的方法:

  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'),
标题:DS.belongsTo('App.Hashtag ')
});

然后对于交易,我们只是改变并提交连接对象。

  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;
}
},标签);
this.get ('hashtagUser')removeObject(itemToRemove);
tag.get('hashtagUser')。removeObject(itemToRemove);
itemToRemove.deleteRecord();
App.store.commit() ;

});



API创建一个HashtagUser对象和跟随方法只是将该用户添加到相关的部分。



要删除,它会弹出关联的对象并销毁关联对象。

$ b虽然它并不像现在那么优雅,但是我们的主要动机是,当Ember Data被更新时,我们应该能够将它转换成一个简单的库存Ember Data支持的版本,比如果我们这个商店本身已经搞砸了。


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 labels.

I am trying to use an adapter that extends the default RESTAdapter. All the application works fine but the double hasMany relationship throws an exception. Digging into the code, a method inverseBelongsToForHasMany throws an exception.

So, I guess that Ember.Data does not support the association of two models with hasMany relationships in both sides and every hasMany requires an associated belongsTo. My questions are:

  1. Is this supported and the issue is just I am doing something wrong?
  2. If it is not supported, is it a feature planned to appear?
  3. Is this a association type to be avoided in this kind of applications? If so, which is the best approach or workaround?

Thanks in advance

解决方案

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.

这篇关于EmberData:与模型相关的两个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆