gpt4 book ai didi

javascript - 克隆记录集 - 使用 Ember Data Canary 构建

转载 作者:行者123 更新时间:2023-11-28 08:50:16 24 4
gpt4 key购买 nike

是否可以克隆(深层复制)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。与网络终端相同。

最佳答案

Ember Data 似乎没有将 DS.belongsTo 关系传播到其 DS.hasMany 对应项,因此我需要将新记录推送到其相应的多个记录中 -关系。我的一些关系是用 { async: true } 定义的,这需要使用 promise 回调来推送新记录。没有定义async的单一关系,即newTerminalObj.location.get('terminals'),需要直接推送记录;使用 Promise 回调会导致记录永远不会被推送,因为在没有 async 定义时,关系缺少 then 方法。

  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
}

关于javascript - 克隆记录集 - 使用 Ember Data Canary 构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19235172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com