2013-09-01 6 views
6

Emberデータ0.13から1.0.0ベータから移行しています。文書https://github.com/emberjs/data/blob/master/TRANSITION.mdによれば、1タイプのアダプタと1タイプのシリアライザがあります。Emberデータ1.0.0:タイプ別のアダプタおよびシリアライザと混同しました

これは、プライマリキーと認証の特定のオーバーライドで「myRestAdapter」を定義できなくなったことを意味します。私はこのコードを各モデルタイプに実装して、同じコードをxx回複製する必要があります。唯一の代わりに_idの_idする主キーを設定するための

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(::エンバーデータ0.13で

コードは

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

コードシームをb電子typeとらわれない、なぜあなただ​​けの、あなたのモデルから拡張することができ、カスタム・シリアライザを作成するようなものはありません:

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(); 
... 

はそれが役に立てば幸い。

+0

良い提案。これに従います!どうも。 Marc – cyclomarc

+0

pertypeアダプタ/シリアライザが定義されていないモデルは、デフォルトのアダプタ/シリアライザにフォールバックされていますか? – 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の主キー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にforループ内のハッシュを切り替えて、スーパーへhashに渡しているということです。これはEmber Data 1.0 Betaのバグでしょうか?

+0

私はハッシュが実際にデータ型の文字列であるため、私はスイッチを実行しなければなりませんでした。私の場合、 'hash'は" movie "を返していました。 – alvincrespo

+0

私の悪いです。私は@intuitivepixelの例から外に出ていた。しかし、emberデータリポジトリのtransition.mdには正しい順序があります。 – alvincrespo

関連する問題