emberjsオブジェクトの配列コントローラーをソートします
-
28-10-2019 - |
質問
この概念を理解しようとしています。 console.log this.get( "content")sortの前後に、すべてが機能したように見えますが、画面に表示するとファンキーになります。問題はハンドルバーにあると思います。 「ソート」すると、重複した4番目のレコードが追加され、上部に固執します。あなたはここで動作している問題を見ることができます:
http://jsfiddle.net/skinneejoe/qpkz5/78/ (レコードにリゾートするために「年齢別」テキストを数回クリックすると、問題が表示されます)
私は何か間違ったことをしていますか、より良い方法がありますか、それともこれはバグですか?それがバグの場合、良い回避策はありますか?
これが完全なコードです:
index.html
<script type="text/x-handlebars">
{{#view App.SortView}}Sort by Age{{/view}}<br/>
{{#each App.userController}}
{{#view App.RecordView contentBinding="this"}}
{{content.name}} - {{content.age}}
{{/view}}
{{/each}}
</script>
app.js
window.App = Ember.Application.create();
App.userController = Ember.ArrayController.create({
content: [
Ember.Object.create({ name:"Jeff", age:24 }),
Ember.Object.create({ name:"Mark", age:32 }),
Ember.Object.create({ name:"Jim", age:12 })
],
sort:"desc",
sortContent:function() {
if (this.get("sort") == "desc") {
this.set("sort", "asc");
} else {
this.set("sort","desc")
}
if (this.get("sort") == "asc") {
var sortedContent = this.get("content").sort( function(a,b){
return a.get("age") - b.get("age");
})
} else {
var sortedContent = this.get("content").sort( function(a,b){
return b.get("age") - a.get("age");
})
}
this.set("content", []);
this.set("content",sortedContent)
}
})
App.RecordView = Ember.View.extend({})
App.SortView = Ember.View.extend({
click: function() {
App.userController.sortContent("poId")
}
})
解決
OS XのSafari、Chrome、またはFirefoxでこのバグは見ていないので、IEの問題だと思います。
よく似ています これにより、エンバーバグが報告されました, 、11日前に修正されました。にアップグレードしてみてください ember-latest そして、それがそれを修正するかどうかを確認します。
所属していません StackOverflow