4

我有一个ArrayController定期更新的。它有一个sorted计算属性,可以让事情保持秩序。我正在尝试使用CollectionView它的content属性绑定到sorted属性,但它没有以正确的顺序呈现它们。演示

我显然做出了错误的假设,即在contentandchildViews属性之间保持顺序。这样做的正确方法是什么?

车把

<script type="text/x-handlebars" data-template-name="item">
    id {{view.content.id}}
</script>

<script type="text/x-handlebars">
    <ul>
     {{collection
        contentBinding="App.itemController.sorted"
        itemViewClass="App.ItemView"
    }} 
    </ul>        
</script>

JavaScript

​App = Ember.Application.create({
    ready: function(){

        // add a new object with a random id
        // every second
        setInterval(function(){

            var id = Math.round(Math.random() * 100),
                obj = Em.Object.create({ id: id });
            App.itemController.pushObject(obj);

        }, 1000);

    }
});

// Collection Item View
App.ItemView = Em.View.extend({
    tagName: "li",
    templateName: "item"
});

App.itemController = Em.ArrayController.create({

    // random order
    content: Em.A([
        Em.Object.create({ id: 5 }),
        Em.Object.create({ id: 3 }),
        Em.Object.create({ id: 10 }),
        Em.Object.create({ id: 6 }),
        Em.Object.create({ id: 1 }),
        Em.Object.create({ id: 2 }),
        Em.Object.create({ id: 100 }),
    ]),

    // filtered content
    sorted: function(){
        return this.get('content').sort(function(a,b){
            return a.get('id') - b.get('id');
        });
    }.property("@each")

});

​</p>

4

2 回答 2

7

为了完整起见@sly7_7 的正确答案,这里有一个带有 的版本CollectionView,请参阅http://jsfiddle.net/pangratz666/ctPAA/

车把

<script type="text/x-handlebars">
    {{collection
        tagName="ul"
        contentBinding="App.itemController"
        itemViewClass="App.ItemView" }} 
</script>

JavaScript

App.itemController = Em.ArrayController.create({

    sortProperties: 'id'.w(),

    // random order
    content: Em.A([
        ...
    ])

});
于 2012-07-06T13:52:34.610 回答
5

受到这个答案的启发:ember.js #each order by property

我使用 ArrayController 的排序属性:https ://github.com/emberjs/ember.js/blob/master/packages/ember-runtime/lib/mixins/sortable.js

我不使用 CollectionView,而是一个简单的#each,并调用 addObject 而不是 pushObject

模板

<script type="text/x-handlebars" data-template-name="item">
    id {{view.content.id}}
</script>

<script type="text/x-handlebars" data-template-name="application">
    <ul>
     {{#each item in App.itemController}}
        {{view App.ItemView contentBinding="item"}}
     {{/each}} 
    <li>        
</script>

javascript

App = Ember.Application.create({
  ready: function() {

    // add a new object with a random id
    // every second
    setInterval(function(){
        var id = Math.round(Math.random() * 100),
            obj = Em.Object.create({ id: id });
        // should call addObject, because pushObject does not sort the inserted item
        //App.itemController.pushObject(obj); 
        App.itemController.addObject(obj);
    }, 1000);
  }       
});

// Collection Item View
App.ItemView = Em.View.extend({
  tagName: "li",
  templateName: "item"
});

App.itemController = Em.ArrayController.create({
  sortProperties: ['id'],
  // random order
  content: Em.A([
    Em.Object.create({ id: 5 }),
    Em.Object.create({ id: 3 }),
    Em.Object.create({ id: 10 }),
    Em.Object.create({ id: 6 }),
    Em.Object.create({ id: 1 }),
    Em.Object.create({ id: 2 }),
    Em.Object.create({ id: 100 }),
  ]),
});
App.ApplicationView = Ember.View.extend({
  templateName: 'application'
});
App.ApplicationController = Ember.Controller.extend();
App.Router = Ember.Router.extend({root: Em.Route.extend()});
App.initialize();​

jsfiddle:http: //jsfiddle.net/Sly7/LJAYk/

于 2012-07-05T22:18:14.673 回答