mysticgohan53
mysticgohan53

Reputation: 415

Emberjs Data Models to JSON

I have the following route:

Loads.TestRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('load');
    }
});

This to the best of my knowledge will return all instances of load in the data store, which in this case can be anywhere from 1 to 100. For this application, I am using a local storage adapter.

My controller looks like this:

Loads.TestController = Ember.ArrayController.extend({

    actions: {
        test: function () {
            var loads = this.get('model');

            var driverId = getCookie("id");
            this.store.find("driver", driverId).then(function (driver,loads) {
                $.ajax({
                    type: "POST",
                    data: JSON.stringify({ Driver: driver, Loads: loads }),
                    url: "api/build",
                    contentType: "application/json",
                    success: function (message) {
                        alert(message);
                    }
                });

            });

        }
    }
});

What I'm trying to accomplish is to send all instances of the model 'load' along with a specific instance of the model driver as a JSON object to an API on my server that builds spreadsheets.

When I run this, I can see in the request payload that the Driver model object is turned into JSON, but the Loads are not. Here is what is in the payload:

Remote Address:[::1]:49438
Request URL:http://localhost:49438/api/build
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:66
Content-Type:application/json
Cookie:id=atcn4
Host:localhost:49438
Origin:http://localhost:49438
Referer:http://localhost:49438/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payloadview source
{Driver: {firstName: "Ron", lastName: "Burgandy", truck: "12"}}

How can I update this to make it so both the driver and loads models are sent in the Payload?

Thanks for any help in advance!

Upvotes: 0

Views: 482

Answers (1)

Jamie Chong
Jamie Chong

Reputation: 842

You need to make sure both promises from your store are resolved before you send off your ajax request. Use Ember.RSVP.hash

Loads.TestController = Ember.ArrayController.extend({
    actions: {
        test: function () {
            var driverId = getCookie("id");

            Ember.RSVP.hash({
                loads: this.store.find('load'),
                driver: this.store.find('driver', driverId)
            }).then(function(data) {
                $.ajax({
                    type: "POST",
                    data: JSON.stringify({ Driver: data.driver, Loads: data.loads }),
                    url: "api/build",
                    contentType: "application/json",
                    success: function (message) {
                        alert(message);
                    }
                });
            });
        }
    }
});

Upvotes: 1

Related Questions