码迷,mamicode.com
首页 > 其他好文 > 详细

multiMap by angular

时间:2015-10-14 12:51:56      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

angular.module(‘demo‘,[])

.factory(‘$$multiMap‘,function(){
    return {
        createNew:function(){
            var map = {};
            
            return {
                entries:function(){
                    return Object.keys(map).map(function(key){
                        return {
                            key:key,
                            value:map[key]
                        }
                    });
                },
                
                get:function(key){
                    return map[key];
                },
                
                hasKey:function(key){
                    return !!map[key];
                },
                
                keys:function(){
                    return Object.keys(map);
                },
                
                put:function(key,value){
                    if(!map[key]){
                        map[key] = [];
                    }
                    
                    map[key].push(value);
                },
                
                remove:function(key,value){
                    var values = map[key];
                    
                    if(!values){
                        return;
                    }
                    
                    var idx = values.indexOf(value);
                    
                    if(idx !== -1){
                        values.splice(idx,1);
                    }
                    
                    if(!value.length){
                        delete map[key];
                    }
                }
            }
        }  
    }       
});


multiMap by angular

标签:

原文地址:http://my.oschina.net/bosscheng/blog/516930

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!