EmberJS排序对象的数组控制器 [英] EmberJS sort Array Controller of Objects

查看:103
本文介绍了EmberJS排序对象的数组控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图想出这个概念。如果你在排序前后使用console.log this.get(content),一切似乎都奏效了,但是当它显示到屏幕上时,它会变得怪异。我认为问题在于把手。当它排序时,它会添加一个重复的第四条记录并将其粘贴在顶部。你可以在这里看到问题:



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

    <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")
    }
})

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆