更新js数据关系对象的正确方法

时间:2016-05-31 09:45:34

标签: javascript jsdata

我有一个相同类型对象的链接列表:

GET /cards.json

[{
  "id": 1,
  "lesserCardId": null,
  "greaterCardId": 2,
 }, {
  "id": 2,
  "lesserCardId": 1,
  "greaterCardId": 3
}, {
  "id": 3,
  "lesserCardId": 2,
  "greaterCardId": null
}]

资源定义:

DS.defineResource({
  name: 'card',
  relations: {
    belongsTo: {
      card: [{
        localField: 'lesserCard',
        localKey: 'lesserCardId'
      }, {
        localField: 'greaterCard',
        localKey: 'greaterCardId'
      }]
    }
  }
});

js-data正确读取json并创建对象层次结构。我接下来想要的是自动更新链接卡属性,如下所示:

var card1 = DS.get('card', 1);
var card4 = DS.inject('card', {'id': 4});
card1.greaterCard = card4;

// corresponding liked object properties should be updated automatically
expect(card4.lesserCard).toBe(card1);
expect(card2.lesserCard).toBeUndefined();

我已经实现了这个,没有js-data制作这样的自定义setter:

Object.defineProperty(Card.prototype, 'greaterCard', {
  set: function (card) {
     // custom logic for updating linked list structure
     if (card === this.$greaterCard) {
       return;
     }
     this.$greaterCard.lesserCard = this.$lesserCard;
     this.$greaterCard = card;
     if (card) {
       card.lesserCard = this;
     }
     // updating depending properties
     this.$updateCardLevel();
     this.$updateCardLevelMax();
  },
  get: function () {
    return this.$greaterCard;
  }
});

但是js-data为关系对象引入了它自己的属性描述符。所以这种方法无法应用。我还没有找到一种方法来挂钩js-data关系属性setter。我可以使用像cardService.setGreaterCard(card, greaterCard);之类的方法来创建一个单独的服务来更新列表结构,但这不会那么方便。是否有更好的方法在属性更改时更新链接对象?

js-data version:2.9.0

1 个答案:

答案 0 :(得分:3)

在JSData 2.x中你会这样做:

var Card = DS.defineResource({
  name: 'card',
  relations: {
    belongsTo: {
      card: [
        {
          localField: 'lesserCard',
          localKey: 'lesserCardId',
          link: false
        },
        {
          localField: 'greaterCard',
          localKey: 'greaterCardId',
          link: false
        }
      ]
    }
  }
});

Object.defineProperties(Card[Card.class].prototype, {
  lesserCard: {
    set: function (lesserCard) {
       this.lesserCardId = lesserCard.id;
       lesserCard.greaterCardId = this.id;
    },
    get: function () {
      return Card.get(this.lesserCardId);
    }
  },
  greaterCard: {
    set: function (greaterCard) {
       this.greaterCardId = greaterCard.id;
       greaterCard.lesserCardId = this.id;
    },
    get: function () {
      return Card.get(this.greaterCardId);
    }
  }
});

在JSData 3.x中你会这样做:

store.defineMapper('card', {
  relations: {
    belongsTo: {
      card: [
        {
          localField: 'lesserCard',
          foreignKey: 'lesserCardId',
          set: function (Relation, card, lesserCard, originalSet) {
            lesserCard.greaterCardId = card.id;
            originalSet(lesserCard);
          }
        },
        {
          localField: 'greaterCard',
          foreignKey: 'greaterCardId',
          set: function (Relation, card, greaterCard, originalSet) {
            greaterCard.lesserCardId = card.id;
            originalSet(lesserCard);
          }
        }
      ]
    }
  }
});