試圖找出這個概念。如果你在排序前後使用console.log this.get(「content」),一切似乎都奏效了,但是當它顯示到屏幕上時,它會變得怪異。我認爲問題在於把手。當它「排序」時,它會添加一個重複的第四條記錄並將其粘貼在頂部。您可以在這裏的行動看這個問題:EmberJS排序對象陣列控制器
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")
}
})
那麼這有什麼問題?點擊排序視圖時,我看不到任何錯誤。 – 2012-02-20 23:00:24
當它「排序」時,它會添加一個重複的第四條記錄並將其粘貼在頂部。對不起,我應該說最初的。我將它添加到我的第一篇文章中。 – skinneejoe 2012-02-21 03:21:23
您正在運行哪個瀏覽器和版本?我在OSX上使用Chrome 17.0.963.56,我沒有看到這第四個記錄。 – 2012-02-21 03:51:11