2016-12-15 76 views
0

v3.rc5 JS-数据HTTP rc.2商店不会缓存嵌套对象

  • 我的API使用嵌套的资源响应e.g /user回应一个嵌套的轮廓。

  • “个人资料” 和 “用户” 是两个映射器与各自的关系

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

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

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

我在做什么错?请帮忙。谢谢。

回答

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));