Ember 数据 1.0.0:与每种类型的适配器和序列化器混淆

2024-03-13

我正在从 Ember 数据 0.13 迁移到 1.0.0 beta。根据文档https://github.com/emberjs/data/blob/master/TRANSITION.md 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, 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 作为主键的模型复制同一个块?是否不再有办法为整个应用程序指定一次?


由于代码接缝是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();
...

希望能帮助到你。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Ember 数据 1.0.0:与每种类型的适配器和序列化器混淆 的相关文章

随机推荐

  • 从外部 html 文件加载handlebars.js 模板没有显示任何内容

    这个问题有点考验我的 JS 技能 所以我可能会像白痴一样解释它 这是我的 JavaScript 用于从服务器获取 json 并尝试将其推送到模板中 Server Interface Start Access the web api for
  • 我可以使用 Xcode 模拟器测试重大变化吗?

    我想知道是否可以在 Xcode Simulator 中测试重大更改位置服务 startMonitoringSignificantLocationChanges 方法 或者它仅适用于实际设备 请注意 我已经在模拟器中尝试过它 但它不起作用 但
  • R 中取消嵌套列表并连接

    我希望取消嵌套 展平 并连接文本中的字符串 逗号分隔 tibble 示例数据 library tidyverse tibble person c Alice Bob Mary score list c Red Green Blue c Or
  • UTC 日期/时间字符串到时区

    如何将 UTC 日期 时间字符串 例如 2011 01 01 15 00 00 转换为 php 支持的任何给定时区 例如 America New York 或 Europe San Marino PHP s DateTime http ph
  • 如何在根据 XML 架构验证 XML 文件时获取错误的行号

    我正在尝试根据 W3C XML 架构验证 XML 以下代码完成该工作并在发生错误时报告 但我无法获取错误的行号 它总是返回 1 有没有简单的方法来获取行号 import java io File import javax xml XMLCo
  • Android 智能手机与其他设备之间的直接 Wifi 通信

    我想在 Android 设备和另一个设备 不是另一个 Android 智能手机 而是使用 C 实现的带 wifi 的设备 之间建立通信 通过 WIFI 我已经发现android提供了直接无线网络 http developer android
  • 获取两个表单以内联方式显示

    这不起作用
  • 在intellij中重新附加源?

    我希望调试到 jaxb impl 的源代码 我下载了版本 2 2 6 附加了源代码 然后意识到我正在调试的应用程序正在使用 jaxb impl 版本 2 2 3 现在我有了正确版本的源代码 2 2 3 我不知道如何从 2 2 6 源代码中删
  • 为什么 PHP 不使用 Internet Explorer 为特定用户保存会话变量?

    我的网站存在问题 其中 PHP 不为使用 Internet Explorer 的特定用户保存会话变量 但对于其他一些使用 Internet Explorer 的用户来说完全没有问题 使用其他浏览器的用户也没有任何问题 我创建了以下三个小脚本
  • 列表框,同步滚动

    我有两个列表框 大小相同 彼此相邻 基本上我用它们来表示链接的项目 类似于 Excel 中的 2 列和多行 其中一个是名字 另一个是姓氏 我想知道是否可以使一个列表框滚动时 另一个列表框与其同步滚动 希望这是有道理的 先谢谢了 None
  • EWS - 如何找到所有未完成的任务?

    我正在使用 Exchange Web 服务尝试获取所有未完成的 Outlook 任务的列表 我有一个 ExchangeService 实例 并尝试查找所有未完成的任务 如下所示 SearchFilter searchFilter new S
  • 数据流/apache beam 窗口中字节数的触发窗口

    我有一个简单的工作 将数据从 pub sub 移动到 gcs pub sub 主题是一个共享主题 具有许多不同大小的不同消息类型 我希望结果在 GCS 中相应地垂直分区 架构 版本 年 月 日 该父键下应该是当天的一组文件 并且文件的大小应
  • 加载后如何获取iframe主体?

    我需要在 iframe 加载后获取 iframe 主体 document getElementById frame contentDocument body假设 iframe 的 ID 是 将以纯 JavaScript 形式提供给您fram
  • 如何通过友好名称打开串口?

    友好名称 显示在 设备管理器 中 端口 COM 和 LPT 下的名称 编辑 下面提供了两个解决方案 一个使用 WMI 另一个使用 SetupAPI 贴出今晚的代码 供大家欣赏 public class SetupDiWrap static
  • 如何为两个Windows窗体应用程序共享一个IsolatedStorage?

    我在一个解决方案中有两个 Windows 窗体应用程序和库 Library类可以在IsolatedStorage中创建新的文件夹和文件 并列出IsolatedStorage中的所有文件和文件夹 第一个应用程序使用库类创建新文件夹 文件 我希
  • 如何在android中将UTC时间戳转换为设备本地时间

    我需要将从服务器获取的 UTC 时间戳转换为本地设备时间 目前 我的时间有 5 小时的差异 例如 当我发布到服务器时 发布时间显示为 5 小时前而不是一秒前 我该如何解决这个问题 下面是我所做的代码 long timestamp curso
  • 在 Spring Security 表单登录页面中获取原始请求 url

    我在我的 spring 安全配置文件中声明了以下内容 http www springframework org schema security spring security 2 0 1 xsd http www springframewo
  • 如何获取TextView的文字颜色?

    在给定的代码中 lbl 0 getTextColor 给出错误 但我不知道如何获取 java 文件中 textview 的文本颜色 请帮助我 public void angry View v if lbl 0 getTextColor Co
  • android中的权限请求代码

    请帮助我获取权限 WRITE EXTERNAL STORAGE 和 WRITE SETTINGS 的请求代码 另请让我知道请求代码的来源 一个简单的演示 Here thisActivity is the current activity i
  • Ember 数据 1.0.0:与每种类型的适配器和序列化器混淆

    我正在从 Ember 数据 0 13 迁移到 1 0 0 beta 根据文档https github com emberjs data blob master TRANSITION md https github com emberjs d