2013-09-01 6 views
6

के साथ उलझन में मैं एम्बर डेटा 0.13 से 1.0.0 बीटा से माइग्रेट कर रहा हूं। दस्तावेज़ https://github.com/emberjs/data/blob/master/TRANSITION.md के अनुसार, अब प्रति प्रकार एडेप्टर और प्रति प्रकार serializers हैं।एम्बर डेटा 1.0.0: प्रति-प्रकार एडाप्टर और सीरियलाइज़र

इसका मतलब है कि अब मैं प्राथमिक कुंजी और प्रमाणीकरण के लिए कुछ विशिष्ट ओवरराइड के साथ "myRestAdapter" को परिभाषित नहीं कर सकता। मुझे प्रत्येक मॉडल प्रकार के लिए अब इस कोड को लागू करने की आवश्यकता है जिसके परिणामस्वरूप xx बार एक ही कोड डुप्लिकेट कर रहा है।

एंबर डेटा 0.13 में कोड:

App.AuthenticatedRestAdapter = DS.RESTAdapter.extend({ 

    serializer: DS.RESTSerializer.extend({ 
    primaryKey: function() { 
     return '_id'; 
    } 
    }), 

    ajax: function (url, type, hash) { 
    hash = hash || {}; 
    hash.headers = hash.headers || {}; 
    hash.headers['Authorization'] = App.Store.authToken; 
    return this._super(url, type, hash); 
    } 
}); 

कोड एंबर में डेटा 1.0.0 (केवल _ id के बजाय _ id के लिए प्राथमिक कुंजी स्थापित करने के लिए:

App.AuthorSerializer = DS.RESTSerializer.extend({ 

    normalize: function (type, property, hash) { 
    // property will be "post" for the post and "comments" for the 
    // comments (the name in the payload) 

    // normalize the `_id` 
    var json = { id: hash._id }; 
    delete hash._id; 

    // normalize the underscored properties 
    for (var prop in hash) { 
     json[prop.camelize()] = hash[prop]; 
    } 

    // delegate to any type-specific normalizations 
    return this._super(type, property, json); 
    } 

}); 

मैं इसे सही है कि समझ में आ गया है मुझे इस मॉडल को हर मॉडल के लिए कॉपी करने की आवश्यकता है जिसके लिए _id को प्राथमिक कुंजी के रूप में आवश्यकता है? क्या पूरे एप्लिकेशन के लिए इसे एक बार निर्दिष्ट करने का कोई तरीका नहीं है?

उत्तर

5

चूंकि कोड बी को सीम करता है

App.Serializer = DS.RESTSerializer.extend({ 

    normalize: function (type, hash, property) { 
    // property will be "post" for the post and "comments" for the 
    // comments (the name in the payload) 

    // normalize the `_id` 
    var json = { id: hash._id }; 
    delete hash._id; 

    // normalize the underscored properties 
    for (var prop in hash) { 
     json[prop.camelize()] = hash[prop]; 
    } 

    // delegate to any type-specific normalizations 
    return this._super(type, json, property); 
    } 

}); 

और फिर अपने सभी मॉडलों के लिए उपयोग करें App.Serializer:

App.AuthorSerializer = App.Serializer.extend(); 
App.PostSerializer = App.Serializer.extend(); 
... 

आशा है कि यह मदद करता है ई type नास्तिक, क्यों तुम सिर्फ अपने कस्टम serializer का निर्माण नहीं करतीं अपने मॉडल से, की तरह कुछ विस्तार कर सकते हैं कि ।

+0

अच्छा सुझाव। इसका पालन करेंगे! धन्यवाद। मार्क – cyclomarc

+0

ऐसे मॉडल करें जिनके पास टाइपटाइप एडेप्टर/सीरियलाइज़र नहीं हैं, अभी भी डिफॉल्ट एडाप्टर/सीरिएलाइज़र पर फ़ॉलबैक परिभाषित हैं? – mehulkar

+0

@MehulKar मुझे डिफ़ॉल्ट एडाप्टर पर फ़ॉलबैक करने की संभावना नहीं मिली है। – cyclomarc

3

आप App.ApplicationSerializer भी सेट कर सकते हैं। यह काम करेगा यदि आप चाहते हैं कि यह सामान्यीकरण प्रत्येक मॉडल पर लागू हो।

App.ApplicationSerializer = DS.RESTSerializer.extend({ 
    normalize: function (type, property, hash) { 
    var json = { id: hash._id }; 
    // ... 
    return this._super(type, property, json); 
    } 
}); 
3

मैं वास्तव में नहीं जानता कि अगर यह सिफारिश की है, लेकिन जब से मैं प्राथमिक कुंजी की जरूरत है हर मॉडल के लिए "_ id" होने के लिए, मैं सिर्फ किया था इस:

DS.JSONSerializer.reopen({ 
    primaryKey: '_id' 
}); 
+0

यह मेरे लिए काम किया। उपरोक्त सुझाए गए उत्तरों नहीं थे। – alvincrespo

0

मैं करने के लिए इस पाया _ id की प्राथमिक कुंजी आईडी के साथ काम करते हैं:

MediaUi.ApplicationSerializer = DS.RESTSerializer.extend({ 

    normalize: function (type, property, hash) { 
    // property will be "post" for the post and "comments" for the 
    // comments (the name in the payload) 

    // normalize the `_id` 
    var json = { id: hash._id }; 
    delete hash._id; 

    // normalize the underscored properties 
    for (var prop in property) { 
     json[prop.camelize()] = property[prop]; 
    } 

    // delegate to any type-specific normalizations 
    return this._super(type, json, hash); 
    } 

}); 

यहाँ अंतर यह है कि मैं property को पाश के लिए में हैश स्विचिंग रहा हूँ और सुपर में hash में गुजर रहा है। शायद यह एम्बर डेटा 1.0 बीटा के साथ एक बग है?

+0

मुझे स्विच करना पड़ा क्योंकि हैश वास्तव में डेटा के प्रकार की एक स्ट्रिंग है, मेरे मामले में 'हैश' "मूवी" लौटा रहा था - जो मॉडल मैं काम कर रहा हूं। – alvincrespo

+0

मेरा बुरा। मैं @intuitivepixel उदाहरण से बाहर जा रहा था। लेकिन एम्बर डेटा रेपो पर transition.md सही ऑर्डर है। – alvincrespo

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