私は以下のヘルパーを抱えています。これは基本的には、私が持っているSVGの束をループし、DBに対応するデータがあれば、SVGの塗りつぶしの色が変わるはずです(データの視覚的表現)。Meteor JS - 過労を避けるためのデザインパターン
Template.patientContentTreatment.helpers({
treatments: function() {
var completedTeeth = $('.svg-tooth-completed > .tooth'); // get all the svgs
var currentNumber, currentPart;
for (var i = 0; i < completedTeeth.length; i++) {
// get a related data-id
currentNumber = $(completedTeeth[i]).closest('.svg-tooth-completed').data('id');
// get the related data-title
currentPart = $(completedTeeth[i]).data('title');
if (Treatments.find({ patient_id: Session.get('currentPatient'), tooth_number: currentNumber, tooth_part: currentPart }).fetch().length) {
// loop through each SVG and change the fill color if correspoding data is founf
$(completedTeeth[i]).css({'fill': '#54a6f8', 'fillOpacity': .8});
}
}
return Treatments.find({patient_id: Session.get('currentPatient')});
}
});
私はまだメテオールの使い方を学んでいます。以前は、私はまだautopublishをインストールして実装していたので、Template.rendered
に添付されていました。 .rendered
- :私はパブ/潜水艦を使用して移動した後ので、しかし、私は私が前に持っていたとして、私はかなりそれを同じように実装することができなかったことがわかりましたテンプレートがとにかくレンダリングされると、サブスクリプションデータはまだ準備されていません。したがって、データは表示されませんでした。私はヘルパーへの機能性を添付しましたので、今
Template.single_patient_treatment_plan.rendered = function() { /* loop through all teeth find if any of them have any findings or treatments attached to them in the database if so, change the fill color */ var completedTeeth = $('.svg-tooth-completed > .tooth'), currentNumber, currentPart; Tracker.autorun(function() { for (var j = 0; j < completedTeeth.length; j++) { currentNumber = $(completedTeeth[j]).closest('.svg-tooth-completed').data("id"); currentPart = $(completedTeeth[j]).data("title"); if (Treatments.find({patient_id: Session.get("current_patient"), tooth_number: currentNumber, tooth_part: currentPart}).fetch().length) { $(completedTeeth[j]).css({"fill": "#54A6F8", fillOpacity: .8}); } } });
、それは非常に間違ったと肥大化した感じ:
は、ここに私の以前の実装です。ヘルパーを使用せずに同じ結果を達成するより良い方法はありますか?
サブスクリプションが準備できていないという問題に対処するには、FlowRouterをご覧ください。 https://github.com/kadirahq/flow-router#subscription-management。 データが不活性である必要がない場合は、Meteorサーバーの方法を試すこともできます。https://guide.meteor.com/methods.html – Jeremiah
SVGはどのように表示されますか?それが別のMeteorテンプレートで行われている場合、そのロジックの大部分はすでに他の場所からレンダリングされたものを修正しようとするのではなく、テンプレートヘルパーに移動する必要があります。 – Waiski
テンプレート(html)コードを表示できますか?私は、配列全体の反応的な再計算を避けながら、多くのdbルックアップを節約する歯のレベルでヘルパーを実装できると考えています。 –