2013-06-17 2 views
1

컬렉션 컬렉션을 관리하는 방법을 찾고 있습니다. 아래 예제 코드를 참조하십시오.컬렉션 컬렉션을 관리하는 방법은 무엇입니까?

function Collection() { 
    this.items = []; //Contains items, which have a date associated with them 
} 
Collection.prototype.doSomethingOnItems = function(){}; 

function SuperCollection() { 
    this.collections = []; //An array of Collection objects 
    this.group = []; //A vector with a string that designates the group (e.g. 2013, 2012) 
} 
SuperCollection.prototype.groupCollections = function(items, groupType) { 
    //Group by year, month, day, etc... 
    //For example, given a timeframe of 2012-2013, items in 2012 are put in collections[1], those from 2013 are in collections[2] 
} 

이와 같은 구조를 관리하는 더 좋은 방법이 있습니까?

+0

답변이 되었습니까? – Metalstorm

답변

0

나는 (jQuery를, 또는 다른 라이브러리를 사용하여

function Collection(items) 
{ 
    // Could/should do some checking/coercion here 
    this.items = items || []; 
}; 

Collection.prototype.add = Collection.prototype.push = function(item) 
{ 
    this.items.push(item); 
}; 

Collection.prototype.remove = function() {} .... 

// etc... 

// A single Group 
function Group(name, items) 
{ 
    this.name = name; 
    this.items = new Collection(items); 
}; 

// A Collection of groups 
function Groups() 
{ 
    this.groups = new Collections(); 
}; 

또는 컬렉션의 프로토 타입 (상속의 한 형태) 예와 함께 그룹 '프로토 타입을 확장 할 수 가능한 일반/추상적 인 사물을 좋아

var groups = new Groups(); 

groups.add(new Group("2013", [])); 
:, 또는 우리 잎 어떤)

function Groups() 
{ 

}; 

$.extend(Groups.prototype, Collection.prototype); 

를 직접 작성

이 모든 것은 당신이 당신의 논리를 분리하고, 당신의 콜렉션 '클래스'와 분리 된 그룹/그룹 '클래스'에 도우미 메서드를 포함 할 수있게 해줍니다.

관련 문제