Emberjs 数据模型 JSON
Emberjs Data Models to JSON
我有以下路线:
Loads.TestRoute = Ember.Route.extend({
model: function() {
return this.store.find('load');
}
});
据我所知,这将 return 加载数据存储中的所有实例,在本例中可以是 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);
}
});
});
}
}
});
我想要完成的是将模型 'load' 的所有实例连同模型驱动程序的特定实例作为 JSON 对象发送到 API我的构建电子表格的服务器。
当我 运行 这样做时,我可以在请求负载中看到 Driver 模型对象变成 JSON,但 Loads 不是。这是有效载荷中的内容:
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"}}
我如何更新它以使其在有效负载中发送驱动程序和负载模型?
提前感谢您的帮助!
在发送 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);
}
});
});
}
}
});
我有以下路线:
Loads.TestRoute = Ember.Route.extend({
model: function() {
return this.store.find('load');
}
});
据我所知,这将 return 加载数据存储中的所有实例,在本例中可以是 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);
}
});
});
}
}
});
我想要完成的是将模型 'load' 的所有实例连同模型驱动程序的特定实例作为 JSON 对象发送到 API我的构建电子表格的服务器。
当我 运行 这样做时,我可以在请求负载中看到 Driver 模型对象变成 JSON,但 Loads 不是。这是有效载荷中的内容:
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"}}
我如何更新它以使其在有效负载中发送驱动程序和负载模型?
提前感谢您的帮助!
在发送 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);
}
});
});
}
}
});