Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save okunokentaro/154a86cef2fbafb1596db5d238f5b71a to your computer and use it in GitHub Desktop.
Save okunokentaro/154a86cef2fbafb1596db5d238f5b71a to your computer and use it in GitHub Desktop.
jquery.atwhoの補完がundefinedになるならisPlainObjectの罠に注意
2015/06/23 にQiitaに投稿した記事のアーカイブです
---
@armorik83です。いま業務で扱っている機能に[jqeury.atwho](https://github.com/ichord/At.js/tree/master)というプラグインを導入しているが、補完候補の全項目が`undefined`になるというバグを踏んで解消に2時間程度持って行かれたので、ここで共有しておく。
- https://github.com/ichord/At.js/blob/183266662aae77a4c4d2a431ce0165f46e125476/dist/js/jquery.atwho.js#L351-L357
```js
if ($.isPlainObject(item)) {
results.push(item);
} else {
results.push({
name: item
});
}
```
`$.isPlainObject(item)`で検証していて、`else`なら勝手に`name`プロパティでネストする。そのためhashが一致せず`undefined`になるという仕組み。
解決方法は自アプリ側にて`JSON.parse(JSON.stringify(item))`として一律で強制的にキャストするのが簡単。単純なのに消耗する罠だった。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment