Ember.js 不使用余烬数据的余烬中的对象关系

Ember.js 不使用余烬数据的余烬中的对象关系,ember.js,ember-cli,Ember.js,Ember Cli,我们在项目中不使用余烬数据。我们得到了一个场景,其中每个模型都有另一种类型模型的id。事实上,我们也没有模型。我们使用的是普通Ajax 让我们进行两个模型的测试和应用。测试将包含为其创建测试的应用程序id。检索测试时,我还需要应用程序数据。当我们使用关系时,默认情况下,余烬数据会执行此操作。没有余烬数据,我如何实现这一点。同一应用程序可能有2个测试。检索到应用程序数据后,我不想再次请求相同的应用程序数据。我的答案-创建自己的内存存储(服务看起来像Ember way解决方案),用于存储已加载的记录

我们在项目中不使用余烬数据。我们得到了一个场景,其中每个模型都有另一种类型模型的id。事实上,我们也没有模型。我们使用的是普通Ajax


让我们进行两个模型的测试和应用。测试将包含为其创建测试的应用程序id。检索测试时,我还需要应用程序数据。当我们使用关系时,默认情况下,余烬数据会执行此操作。没有余烬数据,我如何实现这一点。同一应用程序可能有2个测试。检索到应用程序数据后,我不想再次请求相同的应用程序数据。

我的答案-创建自己的内存存储(服务看起来像Ember way解决方案),用于存储已加载的记录。例如,它可以具有如下结构:

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

它是混合的伪代码和应该可以工作的代码,但是您应该很容易让它与您的应用程序一起工作。:)

我的答案-创建您自己的内存存储(服务看起来像Ember way解决方案),用于存储已加载的记录。例如,它可以具有如下结构:

// 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);
                    });
                }
            });
        });
    }
});
它是混合的伪代码和应该可以工作的代码,但是您应该很容易让它与您的应用程序一起工作。:)