2013-10-26 9 views
62

अपडेट करने के बाद हैस्मी रिश्तों को तोड़ता है मैं स्थानीय भंडारण-एडाप्टर के साथ Ember.js का उपयोग कर रहा हूं। रिकॉर्ड अपडेट करते समय मुझे एक अजीब समस्या है।एम्बर जेएस - अन्य टेबल

App.Post = DS.Model.extend({ 
    title: DS.attr('string'), 
    comments: DS.hasMany('comment', { 
     async: true 
    }) 
}); 
App.Comment = DS.Model.extend({ 
    message: DS.attr('string') 
}); 

ये मेरी पोस्ट और टिप्पणियों नियंत्रकों हैं:: बनाने के रिकॉर्ड hasMany संबंध सही तरीके से अपडेट

App.PostsController = Ember.ArrayController.extend({ 
    newTitle: '', 
    actions: { 
     create: function() { 
      var title = this.get('newTitle'); 
      var post = this.store.createRecord('post', { 
       title: title 
      }); 
      this.set('newTitle', ''); 
      post.save(); 
     } 
    } 
}); 
App.CommentsController = Ember.ArrayController.extend({ 
    needs: "post", 
    post: Ember.computed.alias("controllers.post.model"), 
    newMessage: '', 
    actions: { 
     create: function() { 
      var message = this.get('newMessage'); 
      var comment = this.store.createRecord('comment', { 
       message: message 
      }); 
      var post = this.get('post'); 
      var comments = post.get('comments'); 
      if (comments.get('content') == null) comments.set('content', []); 
      comments.pushObject(comment); 
      comment.save(); 
      post.save(); 
     } 
    } 
}); 

जबकि

मैं hasMany रिश्ते के साथ एक पोस्ट और टिप्पणियों मॉडल है।

{ 
    "App.Post": { 
     "records": { 
      "0v66j": { 
       "id": "0v66j", 
       "title": "post1", 
       "comments": ["p31al", "tgjtj"] 
      } 
     } 
    }, 
    "App.Comment": { 
     "records": { 
      "p31al": { 
       "id": "p31al", 
       "message": "comment 1" 
      }, 
      "tgjtj": { 
       "id": "tgjtj", 
       "message": "comment 2" 
      } 
     } 
    } 
} 

पोस्ट संपादित करते समय समस्या आई। पोस्ट रिकॉर्ड संपादित करने के बाद संबंध खत्म हो गए हैं। मैंने कुछ खोज किया और यह कोड पाया:

DS.JSONSerializer.reopen({ 
    serializeHasMany: function(record, json, relationship) { 
     var key = relationship.key; 
     var relationshipType = DS.RelationshipChange.determineRelationshipType(record.constructor, relationship); 
     // alert(relationshipType); 
     if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany' || relationshipType === 'manyToOne') { 
      json[key] = Ember.get(record, key).mapBy('id'); 
      // TODO support for polymorphic manyToNone and manyToMany 
      // relationships 
     } 
    } 
}); 

यह चाल है और यह ठीक काम करता है। लेकिन अब मुझे एक और समस्या है। अगर मैं किसी अन्य रिकॉर्ड को संपादित, सभी आईडी संदर्भ में इस तरह पूरी वस्तु की जगह:

{"App.Post":{"records":{"0v66j":{"id":"0v66j","title":"post2","comments":[**{"message":"comment 1"}, 
{"message":"comment 2"}**]},"8nihs":{"id":"8nihs","title":"post3","comments":["b4v2b","dbki4"]}}}, 
"App.Comment":{"records":{"p31al":{"id":"p31al","message":"comment 1"},"tgjtj":{"id":"tgjtj","message":"comment 2"}, 
"b4v2b":{"id":"b4v2b","message":"comments3"},"dbki4":{"id":"dbki4", 
"message":"comments4"}}}} 

टिप्पणी refrences टिप्पणियां होना चाहिए ":। [ 'P31al", "tgjtj"] इस तरह लेकिन आईडी "टिप्पणियां" के रूप में प्रतिस्थापित किया गया है: [{"message": "टिप्पणी 1"}, {"संदेश": "टिप्पणी 2"}]

+0

क्या आप केवल गतिशील रूप से टिप्पणी को अपडेट कर सकते हैं? पूरे जेसन ऑब्जेक्ट को वापस करने के बजाय? – yardpenalty

उत्तर

-1

मैंने एम्बर के साथ अपने रास्ते में कुछ चीजें देखी हैं .. और विशेष रूप से एम्बर-डेटा

उनमें से एक है जब एसोसिएशन से निपटने के लिए मुझे मैन्युअल रूप से एसोसिएशन में दोबारा जोड़ना पड़ा फिर से सहेजने के लिए, और इन-मेमोरी एसोसिएशन में addObject का उपयोग करें क्योंकि आप यहां थोड़ा उपयोग कर रहे हैं। :)

ध्यान दें कि यह आमतौर पर तब होता है जब मैं एक से अधिक नई ऑब्जेक्ट को एक बार में अपडेट कर रहा हूं। उदाहरण के लिए, यदि आपकी पोस्ट नई है, और आपकी टिप्पणी भी नई है।

मैं आपके कोडबेस में निम्न कोड देखने के लिए थोड़ा चिंतित हूं, क्योंकि इसे वहां होने की आवश्यकता नहीं है। आपके संगठनों में कभी भी शून्य या गैर-सरणी वस्तुएं नहीं होनी चाहिए। मुझे यकीन है क्या hackery आप एडाप्टर के साथ और क्यों यह जरूरी हो गया था किया था नहीं कर रहा हूँ, लेकिन मुझे आशा है कि कारण नहीं था:

if(comments.get('content') == null) 
    comments.set('content', []); 

वैसे भी, निम्नलिखित कोड को कैसे मैं शायद अपने बनाने कार्रवाई लिखना होता है। यह मदद कर सकता है। मुझे उम्मीद है कि यह करता है।

create: function() { 
    // get the post for association on the new comment 
    var post = this.get('post'); 

    // get the message to store on the new comment 
    var message = this.get('newMessage'); 

    var comment = this.store.createRecord('comment', { 
    message : message, 
    post : post 
    }); 

    comment.save().then(function(savedComment) { 
    post.get('comments').addObject(savedComment); 
    }); 
} 

ध्यान दें कि यह बहुत आसान है। आम तौर पर यदि आप जटिल जटिल चीजें कर रहे हैं, तो कुछ गड़बड़ है और अब मूल बातें वापस जाने का समय है और एक समय में एक चीज जोड़ना, जोड़ों के बीच अच्छी तरह से परीक्षण करना। :)

शुभकामनाएं!

+2

क्या इससे आपकी समस्या में मदद मिली? कैसा चल रहा है? –

1

जब एप्लीकेशन एसरियललाइज़र का उपयोग करते हैं जो LSSerializer को बढ़ाता है, तो ऐसा लगता है।

शायद यह पूछने के बाद तय हो गया है?

संबंधित मुद्दे