2017-03-03 3 views
0

나는 React와 createContainer을 사용하고 있습니다. 나는 두 번의 호출을 함께 할 수있는 방법을 찾고있다. 예를 들어"연결"유성 기부금

, 나는이 데이터가 있다면 : 내 createContainer에서

// Category 
{ 
    _id: 'ABC', 
    name: 'Cat 1' 
} 

// Item 
{ 
    catId: 'ABC', 
    slug: 'slug' 
} 

을, 나는 그것의 슬러그 (Items.find({ slug }))에 의해 Item을 싶어. 나는 그 다음 돌아 서서 item.catId에 의해 카테고리를 얻고 싶다.

나는 이런 식으로 뭔가를 시도했지만 작동하지 않았다 :

createContainer(({ slug }) => { 
    const itemHandler = Meteor.subscribe('Item.bySlug', slug); 
    const item = Items.findOne(); 

    const categoryHandler = Meteor.subscribe('Category.byId', (item.id || {}).id); // also tried just item.id and got undefined for item 
    const category = Categories.findOne(); 

    return { item, category }; 
}, Component); 

내가 item이 잘 얻을 수 있지만, category에는 주사위, 그것은 정의되지 않은 남아. 나는 반응이있는 무엇인가를 촉발시키지 않을 것이라고 확신하지만,이 경우에는 올바른 패턴이 무엇인지, 또는보다 간소화 된 방법이 있는지 확실하지 않다.

+0

이 패키지를 사용할 수 없습니까? https://atmospherejs.com/reywood/publish-composite – ffxsam

답변

0

제가 게시 한 코드가 작동합니다. 데이터가 제대로 표시되지 않았기 때문에 Items.categoryId이 제대로 채워지지 않았습니다.

이 특별한 경우에는 클라이언트 측 조인을하고 싶었고, 내가 한 것은 일을합니다. 항목 비트가 반응하고 일단로드되면 실제로 다시 실행되고 해당 필드가 올바르게 채워집니다.

createContainer(({ slug }) => { 
    const itemHandler = Meteor.subscribe('Item.bySlug', slug); 
    const item = Items.findOne(); 

    const categoryHandler = Meteor.subscribe('Category.byId', (item.id || {}).id); // also tried just item.id and got undefined for item 
    const category = Categories.findOne(); 

    return { item, category }; 
}, Component); 

item을 통해 첫 번째 실행 정의되고 category는 null가됩니다. 다음 번에 (item이 준비 됨) category이 채워집니다. 두 번의 홉을 걸리지 만 잘 작동합니다.

0

당신은 본질적으로 클라이언트 측 조인을하고 있습니다. 이러한 상황에서는 일반적으로 서버 쪽 조인을 수행합니다.

let itemCursor = Items.find({slug: slug}); 

let transformItem = (fields) => { 
    // making an assumption here that Item has a categoryId that indicates its category 
    let category = Categories.findOne(fields.categoryId); 

    // attach the category to the Item 
    fields.category = category; 

    return fields; 
}; 

let handle = itemCursor.observeChanges({ 
    added: (id, fields) => { 
     fields = transformItem(fields); 
     this.added('items', id, fields); 
    }, 
    changed: (id, fields) => { 
     fields = transformItem(fields); 
     this.changed('items', id, fields); 
    }, 
    removed: (id) => { 
     this.removed('items', id); 
    } 
}); 

this.ready(); 

this.onStop(() => { 
    handle.stop(); 
}); 

지금, 클라이언트에서, 당신은 당신이 원하는 상품에 가입 만하면, 그 범주는 첨부됩니다 : 여기의 Item.bySlug 게시자에, 나는 같은 것을 할 것입니다.

3

문제에 대한 가장 간단한 솔루션은 서버 측 publication에서 cursorsarray을 반환하고 subscription 후 클라이언트 측에서 collection.findOne()를 호출하는 것입니다.

Meteor.publish("your.publication.name", function(slug, id){ 
    let itemCursor = Items.find(your_selector); 
    let categoryCursor = Categories.find(your_selector); 
    return [itemCursor, categoryCursor]; 
}); 

이제 클라이언트 측에서 항목 및 카테고리 컬렉션에서 필요한 서류를 얻을 것이다 :

출판 코드는 다음과 같이 될 것이다.

+0

이것은 컬렉션에서 하나의 문서를 게시하고 다른 문서에서 관련 문서를 게시 할 때이 특정 문제에 대한 가장 간단한 해결책입니다. –