1
是否可以在Ember數據中克隆(深層複製)記錄?我已經爲1.0beta之前的版本找到了答案,但是對於最新版本的Ember Data沒有任何答案。克隆記錄集 - 使用Ember數據金絲雀生成
這是我嘗試過什麼(幾乎沒有成功):
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
相同。