2015-10-06 38 views
1

如何得到這個模型的數據:如何獲得相關模型將這些數據與Sails.js + Angular.js和RestAngular

//User.js 
module.exports={ 
name:{ 
type:'string' 
}, 
pets:{ 
collection:'pet', 
via:'owner' 
}, 
cars:{ 
collection:'car', 
via:'owner' 
} 
} 

//Pet.js 
module.exports={ 
name:{ 
type:'string' 
}, 
owner:{ 
model:'user' 
}, 
doctors:{ 
collection:'doctor', 
via:'pets' 
} 
}; 

//Car.js 
module.exports={ 
color:{ 
type:'string' 
}, 
owner:{ 
model:'user' 
} 
} 
//doctor.js 
module.exports={ 
name:{ 
type:'string' 
}, 
pets:{ 
collection:'pet', 
via:'doctors' 
} 
    }; 

使用RESTAPI路線與Restangular消耗,如何讓所有的寵物車主擁有藍色的車?如何使用多個關係(寵物< - 所有者 - >汽車)

使用RestApi路線消耗與Restangular,如何讓所有用戶與寵物有關係與先生XYZ醫生?如何使用多個關係用戶 - >寵物< - >醫生)

感謝您的關注,如果我的問題需要改進,請評論,我會改正任何問題。

回答

2

Deep Queries目前還不支持,有一天。

爲什麼不一樣的東西:

/// Find all blue cars and get their owners 
Cars.find({color:'blue'}).populate('owners').exec(function(err, bluecars) { 

    // unique list of owners 
    var owners = _.uniq(bluecars, function(el) { 
    return el.owner.id; 
    }); 

    // get all complete owners, with pets + cars 
    async.map(
    owners, 
    function(cb) { 
     Owners.findOne({id: el.id}) 
     .populate('pets') 
     .populate('cars') 
     .exec(function(err, data) { 
     cb(err, data); 
     } 
    }, 
    function(err, results) { 
     // this is now a collection of all owners that have blue cars 
     // from this you can see all their pets 
     console.log(results); 
    }); 
}); 

是您做了很多電話到數據庫中,但它是非常簡單和清晰。

一旦性能成爲問題,並希望業主,汽車&寵物都在同一個數據庫上,你可以使用連接編寫自己的查詢。

+1

令人難以置信的代碼人,謝謝!和「async.map(擁有者...」是逗號缺失? – Slinker009

+0

哎呀!修正:) –

相關問題