Ember model的關(guān)聯(lián)關(guān)系處理

2018-01-06 18:01 更新

在前面Ember.js 入門指南之三十八定義模型中介紹過模型之前的關(guān)系。主要包括一對(duì)一、一對(duì)多、多對(duì)多關(guān)系。但是還沒介紹兩個(gè)有關(guān)聯(lián)關(guān)系模型的更新、刪除等操作。

為了測(cè)試新建兩個(gè)模型類。

  1. ember g model post
  2. ember g model comment

1,創(chuàng)建關(guān)系記錄

  1. // app/models/post.js
  2. import DS from 'ember-data';
  3. export default DS.Model.extend({
  4. comments: DS.hasMany('comment')
  5. });
  6. // app/model/comment.js
  7. import DS from 'ember-data';
  8. export default DS.Model.extend({
  9. post: DS.belongsTo('post')
  10. });

設(shè)置關(guān)聯(lián),關(guān)系的維護(hù)放在多的一方comment上。

  1. let post = this.store.peekRecord('post', 1);
  2. let comment = this.store.createRecord('comment', {
  3. post: post
  4. });
  5. comment.save();

保存之后post會(huì)自動(dòng)關(guān)聯(lián)到comment上(保存postid屬性值到post屬性上)。

當(dāng)然啦,你可以在從post上設(shè)置關(guān)聯(lián)關(guān)系。比如下面的代碼:

  1. let post = this.store.peekRecord('post', 1);
  2. let comment = this.store.createRecord('comment', {
  3. // 設(shè)置屬性值
  4. });
  5. // 手動(dòng)吧對(duì)象設(shè)置到post數(shù)組中。(post是多的一方,comments屬性應(yīng)該是保存關(guān)系的數(shù)組)
  6. post.get('comments').pushObject(comment);
  7. comment.save();

如果你學(xué)過Java里的hibernate框架我相信你很容易就能理解這段代碼。你可以想象,post是一的一方,如果它要維護(hù)關(guān)系是不是要把與其關(guān)聯(lián)的commentid保存到comments屬性(數(shù)組)上,因?yàn)橐粋€(gè)post可以關(guān)聯(lián)多個(gè)comment,所以comments屬性應(yīng)該是一個(gè)數(shù)組。

2,更新已經(jīng)存在的記錄

更新關(guān)聯(lián)關(guān)系與創(chuàng)建關(guān)聯(lián)關(guān)系幾乎是一樣的。也是首先獲取需要關(guān)聯(lián)的模型在設(shè)置它們的關(guān)聯(lián)關(guān)系。

  1. let post = this.store.peekRecord('post', 100);
  2. let comment = this.store.peekRecord('comment', 1);
  3. comment.set('psot', post); // 重新設(shè)置comment與post的關(guān)系
  4. comment.save(); // 保存關(guān)聯(lián)的關(guān)系

假設(shè)原來comment關(guān)聯(lián)的postid1的數(shù)據(jù),現(xiàn)在重新更新為comment關(guān)聯(lián)id100post數(shù)據(jù)。

如果是從post方更新,那么你可以像下面的代碼這樣:

  1. let post = this.store.peekRecord('post', 100);
  2. let comment this.store.peekRecord('comment', 1);
  3. post.get('comments').pushObject(comment); // 設(shè)置關(guān)聯(lián)
  4. post.save(); // 保存關(guān)聯(lián)

3,刪除關(guān)聯(lián)關(guān)系

既然有新增關(guān)系自然也會(huì)有刪除關(guān)聯(lián)關(guān)系。 如果要移除兩個(gè)模型的關(guān)聯(lián)關(guān)系,只需要把關(guān)聯(lián)的屬性值設(shè)置為null就可以了。

  1. let comment = this.store.peekRecord('comment', 1);
  2. comment.set('post', null); //解除關(guān)聯(lián)關(guān)系
  3. comment.save();

當(dāng)然你也可以從一的一方移除關(guān)聯(lián)關(guān)系。

  1. let post = this.store.peekRecord('post', 1);
  2. let comment = this.store.peekRecord('comment', 1);
  3. post.get('comments').removeObject(comment); // 從關(guān)聯(lián)數(shù)組中移除comment
  4. post.save();

從一的一方維護(hù)關(guān)系其實(shí)就是在維護(hù)關(guān)聯(lián)的數(shù)組元素。

只要Store改變了Handlebars模板就會(huì)自動(dòng)更新頁面顯示的數(shù)據(jù),并且在適當(dāng)?shù)臅r(shí)期Ember Data會(huì)自動(dòng)更新到服務(wù)器上。

有關(guān)于模型之間關(guān)系的維護(hù)就介紹到這里,它們之間關(guān)系的維護(hù)只有兩種方式,一種是用一的一方維護(hù),另一種是用多的一方維護(hù),相比來說,從一的一方維護(hù)更簡(jiǎn)單。但是如果你需要一次性更新多個(gè)紀(jì)錄的關(guān)聯(lián)時(shí)使用第二種方式更加合適(都是針對(duì)數(shù)組操作)。


博文完整代碼放在Github(博文經(jīng)過多次修改,博文上的代碼與github代碼可能有出入,不過影響不大?。绻阌X得博文對(duì)你有點(diǎn)用,請(qǐng)?jiān)趃ithub項(xiàng)目上給我點(diǎn)個(gè)star吧。您的肯定對(duì)我來說是最大的動(dòng)力??!

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)