1
votes

We don't use Ember-Data in our project. We got a scenario where each model has an id of another type model. In fact we don't have models as well. We are using plain Ajax.

Let's take 2 models Test and Application. A test will contain the application id to which that test is created for. When I retrieve a Test, I also want the application data. Ember-Data does this by default when we use relationships. How can I achieve this without Ember-Data. There might 2 tests for same application. Once the application data is retrieved, I don't want to make a request again for the same application data.

1

1 Answers

0
votes

My answer - create your own in-memory store (Service looks like Ember-way solution) for storing already loaded records. For example it could have structure like this:

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

It's mixed pseudo-code and code that should work, but you should easily get it working with your application. :)