Modifying the value of an object inside an array controller in ember

I have a controller like this. Here in reloadTime function i want to modify the value of a key of the content of this controller.

App.ActivecallsController = Ember.ArrayController.extend({
    activeCalls:[],
    //obj:[],
    deleteCall:function(model){
       var obj = this.findProperty('ID',model.ID);
       App.Cdrglobal.set('active_call',App.Cdrglobal.active_call-1);
       this.removeObject(obj);
    },
    reloadTime:function(){
        var self=this;
        var inter = setInterval(function(){
            $(self.get('content')).each(function(k,v){
               self.set(self.content[k]['duration'],'00');
               clearInterval(inter);
            });
        },1000);
    }
});

the content is

[{"ID":1,"from_number":"1231654","carrier_ip":"123.156.12.1","to_number":"156816548","gw_ip":"156.245.15.12","duration":"12:14"},{"ID":2,"from_number":"1231654","carrier_ip":"123.156.12.1","to_number":"156816548","gw_ip":"156.245.15.12","duration":"15:44"},{"ID":3,"from_number":"1231654","carrier_ip":"123.156.12.1","to_number":"156816548","gw_ip":"156.245.15.12","duration":"52:24"},{"ID":4,"from_number":"1231654","carrier_ip":"123.156.12.1","to_number":"156816548","gw_ip":"156.245.15.12","duration":"52:20"}]

Upvotes: 0

Views: 973

Answers (1)

mavilein
mavilein

Reputation: 11668

I don't really understand, what usecase you are trying to implement there, but here is my try. It is really awkward to see the use of jQuery inside a controller. Instead use the collection methods provided by Ember. As you can see i used the forEach method provided by Ember Arrays.

App.ActivecallsController = Ember.ArrayController.extend({
    activeCalls:[],
    //obj:[],
    deleteCall:function(model){
       var obj = this.findProperty('ID',model.ID);
       App.Cdrglobal.set('active_call',App.Cdrglobal.active_call-1);
       this.removeObject(obj);
    },
    reloadTime:function(){
        var self=this;
        var inter = setInterval(function(){
            self.forEach(function(contentItem){
              self.set("duration", "00");
            });
            clearInterval(inter);
        },1000);
    }
});

This was a basic example to get you started on the Ember Collection methods. In your case it would be even easier to use setEach:

App.ActivecallsController = Ember.ArrayController.extend({
    ...
    reloadTime:function(){
        var self=this;
        var inter = setInterval(function(){
            self.setEach("duration", "00");
            clearInterval(inter);
        },1000);
    }
});

Upvotes: 2

Related Questions