EmberJS中支持单字母单词模型

Are single-lettered word models supported in EmberJS?

本文关键字:单词 模型 单字母 支持 EmberJS      更新时间:2023-09-26

我真的无法确定问题是Ember还是Ember数据,或者它是否是一个问题,但情况如下:

假设您的模型名为tell_me_a_story。如果您使用ActiveModelAdapter,这将是JSON应该提供的名称。

无论如何,当Ember或EmberData在内部处理它时,它会将它客串化,并成为tellMeAStory,这正确地表明"A"answers"Story"是两个独立的词。

然而,当在内部对其进行去melize以查找模型时,去melize函数会将其转换为tell_me_astory

在我看来,最后的行为似乎有缺陷,但当查看派生出这种行为的测试时,它实际上是为了以这种方式管理缩写词。(将下面的例子与"innerHtml"进行比较,这是我对驼色大小写多字母缩写词的期望。)

QUnit.test("converts a camelized string into all lower case separated by underscores.", function() {
  deepEqual(decamelize('innerHTML'), 'inner_html');
  if (Ember.EXTEND_PROTOTYPES) {
    deepEqual('innerHTML'.decamelize(), 'inner_html');
  }
});

(Ember运行时中的来源)

那么,在带有Ember的模型中使用单字母单词的正确方式是什么?他们甚至得到支持了吗


下面是我尝试做的一个例子:

// this comes from a separate data source, e.g. REST APIs
var myModelName = 'tell_me_a_story';
// this line will throw if the model file is named "tell-me-a-story"
if (!store.getById(myModelName, 1)) {
  // this line will throw if the model file is named "tell-me-astory"
  store.pushPayload(myModelName, myObject);
}

您可以覆盖存储_normalizeTypeKey,然后更改camelBase行为,使其成为您想要的(例如,dasherized或只修复这种情况)。

当你走另一条路时,你也可以覆盖串行器typeForRoot——这让你告诉ember数据中特定密钥(例如tell_me_a_story)的模型密钥是什么(例如tellMeAStory)。

似乎有工作正在进行中,以使一切都像容器一样工作(已dasherized)