Is it possible to clone (deep-copy) a record in Ember Data? I've found answers for pre-1.0beta, but nothing for the recent versions of Ember Data.
This is what I've tried (with little to no success):
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
}
Any way I try it, newSnapshot.get('regions.length')
ends up being 0
. The same with networks
and terminals
.