Ember数据1.0.0:与每种类型的适配器和序列化程序混淆 [英] Ember data 1.0.0: confused with per-type adapters and serializers

查看:98
本文介绍了Ember数据1.0.0:与每种类型的适配器和序列化程序混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Ember数据0.13迁移到1.0.0 beta。根据文档 https://github.com/emberjs/data/blob/master/TRANSITION.md ,现在有每种类型的适配器和每个类型的序列化程序。



这意味着我不能再为主键定义一个myRestAdapter和认证。我需要为每个模型类型实现此代码,导致重复xx次相同的代码。



Ember数据中的代码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);
}
});

Ember数据1.0.0中的代码(仅用于将主键设置为_id而不是_id:

  App.AuthorSerializer = DS.RESTSerializer.extend({

normalize:function(type,property,哈希){
//属性将为该帖子的post和
//注释的注释(有效负载中的名称)

//规范化`_id`
var json = {id:hash._id};
delete hash._id;

//归一化下划线属性
for(var prop in哈希){
json [prop.camelize()] = hash [prop];
}

//委托给任何类型特定的规范化
返回这个。 _super(type,property,json);
}

});



我的理解是正确的,我需要复制这个相同的块,对于每个需要_id作为主键的模型,不再有一种方式为整个应用程序指定一次?

解决方案

由于代码接缝为类型不可知,为什么不要创建自定义序列化器模型可以从以下内容扩展:

  App.Serializer = DS.RESTSerializer.extend({

normalize:function(type,hash,property){
//属性将被发布为post,而
//注释的注释(有效负载中的名称)

//规范化`_id`
var json = {id:hash._id};
delete hash._id;

//规范化下划线的属性
for(var prop in hash){
json [prop.camelize()] = hash [prop];
}

//委托给任何类型特定的规范化
返回this._super(type,json,property);
}

});

然后使用 App.Serializer 您的模型:

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

希望有帮助。


I am migrating from Ember data 0.13 to 1.0.0 beta. According to the doc https://github.com/emberjs/data/blob/master/TRANSITION.md, there are now per type adapters and per type serializers.

This means that I can no longer define a "myRestAdapter" with some specific overrides for the primary key and the authentication. I need to implement this code now for each model type resulting in duplicating xx times the same code.

Code in Ember data 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);
  }
});

Code in Ember data 1.0.0 (only for setting the primary key to _id instead of _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);
  }

});

Have I understood it correct that I need to copy this same block now for every model that requires the _id as primary key ? Is there no longer a way to specify this once for the whole application ?

解决方案

Since the code seams to be type agnostic, why you don't just create your custom serializer that your models can extend from, something like:

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);
  }

});

And then use App.Serializer for all your models:

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

Hope it helps.

这篇关于Ember数据1.0.0:与每种类型的适配器和序列化程序混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆