我有以下路线:
Loads.TestRoute = Ember.Route.extend({
model: function() {
return this.store.find('load');
}
});
据我所知,这将返回数据存储中的所有加载实例,在本例中可以是1到100之间的任何位置。对于此应用程序,我使用的是本地存储适配器。
我的控制器看起来像这样:
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);
}
});
});
}
}
});
我想要完成的是发送模型的所有实例' loading'以及模型驱动程序的特定实例,作为我的服务器上构建电子表格的API的JSON对象。
当我运行它时,我可以在请求有效负载中看到Driver模型对象变为JSON,但负载不是。这是有效载荷中的内容:
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"}}
如何更新此设置,以便在Payload中发送驱动程序和加载模型?
提前感谢您的帮助!
答案 0 :(得分:1)
在发送ajax请求之前,您需要确保商店中的所有承诺都已解决。使用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);
}
});
});
}
}
});