2013-10-07 2 views
1

Ember Data에서 레코드를 복제 (deep-copy) 할 수 있습니까? 나는 1.0beta 이전 버전에 대한 답을 찾았지만 Ember Data의 최신 버전에는 아무 것도 없다.복제본 레코드 - Ember 데이터 사용 Canary 빌드

이것은 내가 (성공없이 거의없이) 시도했습니다 것입니다 :

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되는 끝납니다. networksterminals과 동일합니다.

답변

0

Ember Data가 DS.belongsTo 관계를 자신의 DS.hasMany 개까지 전파하지 않는 것처럼 보이므로 새로운 레코드를 해당되는 많은 관계로 푸시해야했습니다. 내 관계 중 일부는 { async: true }으로 정의되었으므로 약속 레코드 콜백을 사용하여 새 레코드를 푸시해야했습니다. async없이 정의 된 한 관계, 즉 newTerminalObj.location.get('terminals')은 레코드를 직접 푸시해야합니다. 약속 콜백을 사용하면 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 
    } 
관련 문제