0

我想对我的内容中的嵌入记录进行排序。最初,我将嵌入的记录分离为 ArrayController 并对其进行了排序——这非常简单,但现在我被告知我应该只使用没有 ArrayController 的嵌入记录。我按照http://www.javascriptkit.com/javatutors/arraysort2.shtml对数组对象进行排序,内容正在排序,但视图没有相应更新。我的功能看起来像:

    setSort: function (sort) {
        var sortedContent = this.get('content.analyticsRunParameters');
        sortedContent.sort(function(a, b){
            var colA=a.get(sort).toLowerCase(), colB=b.get(sort).toLowerCase();

            if (colA < colB) //sort string ascending
                return -1;
            if (colA > colB)
                return 1;
            return 0; //default return value (no sorting)
        });
        this.set('content.analyticsRunParameters',sortedContent);

        console.log(sortedContent);//is sorted
        console.log(this.get('content.analyticsRunParameters'));//is sorted

    }

对我的内容进行排序时,有没有办法更新视图?还是使用 ArrayController 的唯一方法?谢谢。

4

1 回答 1

0

I found the solution in another post here : Ember.ArrayProxy changes not triggering handlebars #each update

I don't know if this is a best solution either. It seems like I should be just able to sort the array objects without the help of array proxy. I had to make minor modification in the transform I was using to return ArrayProxy instead of normal array of objects like :

AS.AnalyticsRunParameterTransform = DS.Transform.extend({
//return array of ember objects
deserialize: function (serialized) {
    var objects = [];
    for (var key in serialized) {
        objects.push(Ember.Object.create({
            "name": serialized[key].name,
            "description": serialized[key].description,
            "default": serialized[key]["default"],
            "value": serialized[key].value,
            "category": serialized[key].category
        }));
    }

    //return objects;
    return Ember.ArrayProxy.create({ content: objects });
},
//return JSON object
serialize: function (deserialized) {
    var analyticsTemplate = {}, object;
    for (var i = 0, len = deserialized.length; i < len; i++) {
        object = deserialized[i];
        analyticsTemplate[object.get('name')] = {"name": object.get('name'),
            "description": object.get('description'),
            "default": object.get('default'),
            "value": object.get('value'),
            "category": object.get('category')};
    }
    return analyticsTemplate;
}
});
于 2013-10-07T13:50:48.777 回答