我们在项目中不使用Ember-Data。我们有一个场景,每个模型都有一个不同类型的模型的id。事实上,我们也没有模型。
让我们来看两个模型的测试和应用。测试将包含为其创建测试的应用程序id。当我检索测试时,我还需要应用程序数据。当我们使用关系时,Ember-Data默认会这样做。没有Ember-Data,我如何才能做到这一点。对于同一应用程序,可能有两个测试。一旦检索到应用程序数据,我就不想再次请求相同的应用程序数据。
发布于 2017-06-07 05:51:17
我的答案是-创建您自己的内存存储(服务看起来像Ember解决方案)来存储已经加载的记录。例如,它可以具有这样的结构:
// app/services/store.js
export default Ember.Service.extend({
ajax: Ember.inject.service(),
store: {
applications: [],
tests: []
},
getTest(id) {
const store = this.get('store');
return new Ember.RSVP.Promise(resolve => {
const loadedTest = store.tests.findBy('id', id);
if (loadedTest) {
return resolve(loadedTest);
}
this.get('ajax').getRequest(urlForGettingTestWithConcreteId).then(test => {
if (test.application) {
const application = store.applications.findBy('id', test.application);
if (application) {
test.application = application;
store.tests.pushObject(test);
return resolve(test);
}
this.get('ajax').getRequest(test.application).then(application => {
test.application = application;
store.applications.pushObject(application);
store.tests.pushObject(test);
resolve(test);
});
}
});
});
}
});它是混合的伪代码和应该可以工作的代码,但您应该很容易让它与您的应用程序一起工作。:)
https://stackoverflow.com/questions/44399939
复制相似问题