是否可以在Ember Data中克隆(深层复制)记录?我已经找到了1.0beta之前的答案,但是最近版本的Ember Data没有找到答案。
这是我尝试过的(几乎没有成功):
attributesOf: function(record) {
var attributes = record.get('constructor.attributes')
, newRecordAttributes = {}
attributes.forEach(function(attribute) {
newRecordAttributes[attribute] = record.get(attribute)
})
return newRecordAttributes
}
, cloneSnapshot: function(snapshot) {
var that = this
, regions = snapshot.get('regions')
, networks = snapshot.get('networks')
, terminals = snapshot.get('terminals')
, scenario = snapshot.get('scenario')
, newSnapshot = this.store.createRecord('snapshot', {
name: snapshot.get('name')
, timestamp: Date.now()
, autosave: false
, fresh: true
})
, newRegions = regions.map(function(region) {
var newRegionObj = that.attributesOf(region)
newRegionObj.snapshot = newSnapshot
var test = that.store.createRecord('region', newRegionObj)
return test
})
, newNetworks = networks.map(function(network) {
var newNetworkObj = that.attributesOf(network)
newNetworkObj.snapshot = newSnapshot
return that.store.createRecord('network', newNetworkObj)
})
, newTerminals = terminals.map(function(terminal) {
var newTerminalObj = that.attributesOf(terminal)
newTerminalObj.snapshot = newSnapshot
newTerminalObj.location = newRegions.filterProperty('name', terminal.get('location.name'))[0]
newTerminalObj.network = newNetworks.filterProperty('name', terminal.get('network.name'))[0]
return that.store.createRecord('terminal', newTerminalObj)
})
Ember.RSVP.all([newSnapshot, newRegions, newNetworks, newTerminals]).then(function(records) {
records[0].get('regions').pushObjects(newRegions)
records[0].get('networks').pushObjects(newNetworks)
records[0].get('terminals').pushObjects(newTerminals)
}) // doesn't work
// newSnapshot.set('regions', newRegions) // doesn't work
// newSnapshot.set('networks', newNetworks) // doesn't work
// newSnapshot.set('terminals', newTerminals) // doesn't work
// newSnapshot.get('regions').pushObjects(newRegions) // doesn't work
// newSnapshot.get('networks').pushObjects(newNetworks) // doesn't work
// newSnapshot.get('terminals').pushObjects(newTerminals) // doesn't work
return newSnapshot
}
我尝试任何方式,newSnapshot.get('regions.length')
最终都是0
。与networks
和terminals
相同。
答案 0 :(得分:0)
Ember Data似乎没有将DS.belongsTo
关系传播给他们的DS.hasMany
对应关系,因此我需要将新记录推送到相应的多关系中。我的一些关系是用{ async: true }
定义的,它需要使用promise回调来推送新记录。没有async
定义的一个关系,即newTerminalObj.location.get('terminals')
,需要直接推送记录;使用promise回调导致记录永远不会被推送,因为在没有then
的情况下定义时关系缺少async
方法。
attributesOf: function(record) {
var attributes = record.get('constructor.attributes')
, newRecordAttributes = {}
attributes.forEach(function(attribute) {
newRecordAttributes[attribute] = record.get(attribute)
})
return newRecordAttributes
}
, cloneSnapshot: function(snapshot) {
var that = this
, regions = snapshot.get('regions')
, networks = snapshot.get('networks')
, terminals = snapshot.get('terminals')
, scenario = snapshot.get('scenario')
, newSnapshot = this.store.createRecord('snapshot', {
name: snapshot.get('name')
, timestamp: Date.now()
, autosave: false
, fresh: true
})
, newRegions = regions.then(function(array) {
return array.map(function(region) {
var newRegionObj = that.attributesOf(region)
newRegionObj.snapshot = newSnapshot
return that.store.createRecord('region', newRegionObj)
})
})
, newNetworks = networks.then(function(array) {
return array.map(function(network) {
var newNetworkObj = that.attributesOf(network)
newNetworkObj.snapshot = newSnapshot
return that.store.createRecord('network', newNetworkObj)
})
})
, newTerminals = Ember.RSVP.all([terminals, newRegions, newNetworks]).then(function(arrays) {
return arrays[0].map(function(terminal) {
var newTerminalObj = that.attributesOf(terminal)
newTerminalObj.snapshot = newSnapshot
newTerminalObj.location = arrays[1].filterProperty('name', terminal.get('location.name'))[0]
newTerminalObj.network = arrays[2].filterProperty('name', terminal.get('network.name'))[0]
var newTerminal = that.store.createRecord('terminal', newTerminalObj)
newTerminalObj.location.get('terminals').pushObject(newTerminal)
newTerminalObj.network.get('terminals').then(function(array) {
array.pushObject(newTerminal)
})
return newTerminal
})
})
Ember.RSVP.all([newSnapshot.get('regions'), newRegions]).then(function(arrays) {
arrays[0].pushObjects(arrays[1])
})
Ember.RSVP.all([newSnapshot.get('networks'), newNetworks]).then(function(arrays) {
arrays[0].pushObjects(arrays[1])
})
Ember.RSVP.all([newSnapshot.get('terminals'), newTerminals]).then(function(arrays) {
arrays[0].pushObjects(arrays[1])
})
return newSnapshot
}