简体   繁体   English

如何与Ember.js和ember-data创建has_and_belongs_to_many关系?

[英]How to create has_and_belongs_to_many relationship with Ember.js & ember-data?

Is it possible to create a hasAndBelongsToMany relationship with Ember.js & ember-data? 是否可以与Ember.js和ember-data创建hasAndBelongsToMany关系?

Edit: Added ActiveRecord model examples to clarify. 编辑:添加ActiveRecord模型示例以澄清。

class Project < ActiveRecord::Base
  has_and_belongs_to_many :tags
end

class Tag < ActiveRecord::Base
  has_and_belongs_to_many :projects
end

I have an associative table "projects_tags" linking project_id <=> tag_id. 我有一个关联表“projects_tags”链接project_id <=> tag_id。

In this example, I'd suggest embedding the tags in the Post, so that the resulting structure looks like this 在这个例子中,我建议在Post中嵌入标签,以便生成的结构如下所示

{
   title: "some post",
   tags: [ "tag1", "tag2" ]
}

This is generally good practice when you don't need to have additional value on the relation. 当您不需要对关系有额外的价值时,这通常是一种很好的做法。 If you want to have the Tag model on the client, you can still do it via embedding. 如果您想在客户端上使用Tag模型,您仍然可以通过嵌入来实现。

{
   title: "some post",
   tags: [ { name: "first tag" }, { name: "second tag" } ]
}

in this case you could specify it as an embedded relation, such as this 在这种情况下,您可以将其指定为嵌入式关系,例如

App.Post = DS.Model.extend({
  title: DS.attr("string"),
  tags: DS.hasMany("App.Tag")
});

App.Tag = DS.Model.extend({
  name: DS.attr("string"),
  post: DS.belongsTo("App.Post")
});

App.store.adapter.serializer.map("App.Tag", {
  tags: { embedded: 'load' }
});

keep in mind that embedding only works in Ember Data revision 10. It is not supported in 5-9. 请记住,嵌入仅适用于Ember Data修订版10.在5-9中不支持。

Most of the time you don't really want to model everything the same way as you would model it in a relation database. 大多数情况下,您并不希望以与在关系数据库中对其进行建模相同的方式对所有内容进行建模。

You should lean towards NoSQL-ish JSON, which generally means embedding things, rather than making complex associations. 你应该倾向于NoSQL-ish JSON,这通常意味着嵌入东西,而不是复杂的关联。 The reason for this is that your database can JOIN data very efficiently, especially with indexes, but doing this over the network is a bad idea because it results in a lot of requests. 这样做的原因是您的数据库可以非常有效地JOIN数据,尤其是索引,但通过网络执行此操作是一个坏主意,因为它会导致大量请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ember.js ember-data findAll远程服务器上的对象 - Ember.js ember-data findAll objects on the remote server 在has_and_belongs_to_many关系中记录数据 - Recording data with has_and_belongs_to_many relationship in rails 如何创建一个新的子对象,然后以相同的形式与该子对象创建has_and_belongs_to_many关系 - How to create a new child object and then create a has_and_belongs_to_many relationship with that child object in the same form Ember.js错误:使用ember-data保存记录时未找到&#39;id&#39;的模型 - Ember.js Error: No model was found for 'id' while saving record using ember-data 使用Ember.js和Ember-data&Rails创建记录并处理记录列表 - creating a record with Ember.js & Ember-data & Rails and handling list of records ember.js实例化路由器中的ember-data查询的第一条记录 - ember.js instantiate first record from ember-data query in router 如何从has_and_belongs_to_many模型关系访问表 - How to access a table from a has_and_belongs_to_many model relationship 如何设置 has_and_belongs_to_many 与范围和别名 class 的关系? - How to setup has_and_belongs_to_many relationship with a scoped and aliased class? has_and_belongs_to_many关系的Rails路由 - Rails routing for has_and_belongs_to_many relationship 在Rails中创建has_and_belongs_to_many关系 - Creating a has_and_belongs_to_many relationship in Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM