[英]model.save() timing issue in ember-data with belongsTo relationship
I have two models ( source
and problem
) and I am saving an instance with relation to the other when click
is triggered through the UI. 我有两个模型(
source
和problem
),并且通过UI触发click
时,我正在保存一个与另一个实例相关的实例。
App.SourcesController = Ember.ArrayController.extend
actions:
addProblem: (source) ->
problem = @store.createRecord('problem',
detectedOn: new Date(),
source: source
)
problem.save().then (problem) ->
# handle success
,(e) ->
# handle error
false
I am certain that source
is present when inside the addProblem
action, but when the client actually serializes the model and sends the request, detectedOn
attribute is present, but source_id
is nowhere to be found. 我确定在
addProblem
动作中存在source
,但是当客户端实际序列化模型并发送请求时,存在detectedOn
属性,但是无法找到source_id
。
Now, here is the interesting part. 现在,这是有趣的部分。
When I wrap the save code in setTimeout
, both detectedOn
and source_id
do get sent to the server: 当我将保存代码包装在
setTimeout
, detectedOn
和source_id
都将发送到服务器:
App.SourcesController = Ember.ArrayController.extend
actions:
addProblem: (source) ->
problem = @store.createRecord('problem',
detectedOn: new Date(),
source: source
)
setTimeout ->
problem.save().then (problem) ->
# handle success
,(e) ->
# handle error
, 1
false
It seems to me that it's a timing issue within Ember's cycle, or perhaps I am missing something? 在我看来,这是Ember周期内的计时问题,还是我错过了一些东西?
How can I get rid of the setTimeout
? 我如何摆脱
setTimeout
? I should not be doing this every time I save. 我不应该在每次保存时都这样做。
I solved this by setting the inverse relationship manually when resolving the promise.The following should work for you: 我通过在解决承诺时手动设置逆向关系来解决此问题,以下内容应为您工作:
App.SourcesController = Ember.ArrayController.extend
actions:
addProblem: (source) ->
problem = @store.createRecord('problem',
detectedOn: new Date(),
source: source
)
problem.save().then (problem) ->
source.get('problems').addObject(problem)
,(e) ->
# handle error
false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.