EmberJS排序对象的数组控制器 [英] EmberJS sort Array Controller of Objects
问题描述
http://jsfiddle.net/skinneejoe/Qpkz5/78/ (点击'按年龄排序'文本几次以求出记录,你会看到问题)
我做错了什么,有没有更好的方法,还是这是一个错误?如果这是一个错误,是否有一个很好的解决方法?
以下是完整的代码:
$ b index.html p>
< script type =text / x-handlebars>
{{#view App.SortView}}按年龄{{/ 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 sort.get(sort)==asc){
var sortedContent = this.get(content)。sort(function(a,b){
return a.get(年龄) - 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({
点击:function(){
App.userController.sortContent(poId)
}
})
我没有在OS X上看到Safari,Chrome或Firefox的这个bug,所以我认为这是一个IE问题。 b
$ b
听起来很像这个报告的Ember bug ,这在11天前得到修复。尝试升级到并查看是否修复了该问题。 Trying to figure this concept out. If you console.log this.get("content") before and after the sort, everything seems like it worked, but when it displays to the screen it gets funky. I think the issue is with Handlebars. When it "sorts" it adds a duplicate fourth record and sticks it at the top. You can see the problem in action here: http://jsfiddle.net/skinneejoe/Qpkz5/78/ (Click the 'Sort by Age' text a couple times to resort the records and you'll see the issues) Am I doing something wrong, is there a better way, or is this a bug? If it's a bug is there a good workaround? Here's the full code: index.html app.js
I'm not seeing this bug in Safari, Chrome or Firefox on OS X, so I assume it's a IE issue. Sounds a lot like this reported Ember bug, which got fixed 11 days ago. Try upgrading to ember-latest and see if that fixes it. 这篇关于EmberJS排序对象的数组控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! <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>
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")
}
})