修改ember中数组控制器内对象的值

时间:2013-04-09 09:42:15

标签: ember.js

我有这样的控制器。在reloadTime函数中,我想修改该控制器内容的键的值。

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

内容

[{"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"}]

1 个答案:

答案 0 :(得分:2)

我真的不明白,你试图在那里实现什么用法,但这是我的尝试。看到在控制器中使用jQuery真的很尴尬。而是使用Ember提供的收集方法。如您所见,我使用了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);
    }
});

这是让您开始使用Ember Collection方法的基本示例。在你的情况下,to use setEach会更容易:

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