试图找出这个概念。如果你在排序前后使用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