存储不会缓存嵌套对象

时间:2016-12-15 00:33:58

标签: jsdata

v3.rc5 js-data-http rc.2

  • 我的API使用嵌套资源进行响应,例如/user使用嵌套配置文件进行响应。

  • “profile”和“user”是两个具有相应关系的映射器

"user": { "id": 1, "name": "Peter", "profile": { "id": 5 "dob": "today" } }

“profile”不会添加到缓存中。我可以调用user.profile,但store.cachedFind('profile', 5)返回undefined。

手动调用 store.addToCache('profile', user.profile) 不会抛出任何错误,但也不会将其添加到缓存中。

我做错了什么?请帮忙。感谢。

1 个答案:

答案 0 :(得分:1)

以下是一个工作示例(关键是定义userprofile之间的关系):

import { DataStore } from 'js-data';

const store = new DataStore();

store.defineMapper('user', {
  relations: {
    hasOne: {
      profile: {
        foreignKey: 'userId',
        localField: 'profile'
      }
    }
  }
});

store.defineMapper('profile', {
  relations: {
    belongsTo: {
      user: {
        foreignKey: 'userId',
        localField: 'user'
      }
    }
  }
});

const user = store.add('user', {
  id: 123,
  profile: {
    id: 345,
    userId: 123
  }
});

console.log(store.get('user', 123));
console.log(store.get('profile', 345));
console.log(user.profile === store.get('profile', 345));
相关问题