2

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

获取 /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 像这样制作自定义设置器:

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 关系属性设置器。我可以使用cardService.setGreaterCard(card, greaterCard);更新列表结构的方法创建一个单独的服务,但这不会那么方便。有没有更好的方法来更新属性更改的链接对象?

js数据版本:2.9.0

4

1 回答 1

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);
          }
        }
      ]
    }
  }
});
于 2016-10-21T16:58:59.533 回答