0
votes

I want to sort embedded records in my content. Initially I had separated the embedded record as an ArrayController and did sorting on it - it was pretty straight forward, but now I am told that I should use just embedded records without ArrayController. I followed http://www.javascriptkit.com/javatutors/arraysort2.shtml to sort the array objects and the content is getting sorted but the view is not getting updated accordingly. My function looks like :

    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

    }

Is there a way to update the view when my content is sorted? Or using ArrayController the only way around? Thanks.

1

1 Answers

0
votes

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