[英]Send additional data to the server with ember-data
I've to save a new message
onto the server and also send additional data to it 我必须将
message
保存到服务器上,并向其发送其他数据
I've the following code 我有以下代码
App.Message = DS.Model.extend({
body: DS.attr('string'),
messagethread_id: DS.attr('number')
});
// in controller
var msg = this.store.createRecord('message', {
body: messageBody,
messagethread_id: thread.id,
token: this.get('token')
});
msg.save();
As you can see the token isn't defined on the model so it isn't sent to the server. 如您所见,令牌未在模型上定义,因此不会发送到服务器。 I need to send it but I don't need to retrieve that in any situation.
我需要发送它,但是在任何情况下都不需要检索它。
thanks in advance 提前致谢
The simplest thing: why don't you define also token in your model? 最简单的事情:为什么不在模型中也定义标记? Then you manage it server-side;
然后在服务器端对其进行管理; for example when the token is not needed, you set it to null;
例如,当不需要令牌时,将其设置为null; in the server you can check if it is null or not;
在服务器中,您可以检查它是否为空; of course then your server will have to answer to GET request also with the token attribute (but you simply set it to null too);
当然,然后您的服务器也必须使用token属性来响应GET请求(但您也可以将其设置为null);
or you can define two different models: a Message Model and a MessageWithToken Model; 或者,您可以定义两个不同的模型:消息模型和MessageWithToken模型; and then use the first one or the last one depending if you need to send token or not;
然后根据您是否需要发送令牌使用第一个或最后一个; also in this case you will have to manage things in your server.
同样在这种情况下,您将必须管理服务器中的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.