Skip to content

Instantly share code, notes, and snippets.

@winkerVSbecks
Last active December 31, 2015 15:19
Show Gist options
  • Select an option

  • Save winkerVSbecks/8006356 to your computer and use it in GitHub Desktop.

Select an option

Save winkerVSbecks/8006356 to your computer and use it in GitHub Desktop.
Meteor prevent re-loading data on each edit?
// TemplateBase.inspectionOrderFromService() is inspectionService.js
Template.assemble.items = function(result) {
var order = TemplateBase.inspectionOrderFromService();
if(order) {
return order.getInspectionItemsWithResult(result);
}
};
// AutoEd is an object with a list of all available assets (images, vids and animations)
Template.detailedItem.educationAssets = function() {
var itemKey = this.key;
var assetIds = InspectionGroups.getItemEducationAssets(itemKey);
var itemAssets = AutoEd.assets.filter(function(asset){
return assetIds.indexOf(asset.key) >= 0;
});
var selectedIds = []
var inspectionOrder = TemplateBase.inspectionOrderFromService();
if(inspectionOrder) {
selectedIds = inspectionOrder.getEducationAssetsForItem(itemKey);
}
var wrappedItems = itemAssets.map(function(asset) {
var isSelected = selectedIds.indexOf(asset.key) >= 0;
return {
asset: asset,
groupKey: this.groupKey,
itemKey:itemKey,
selected:isSelected
};
});
var result = Template.assemble.isReviewOrReport()?wrappedItems.filter(function(item){return item.selected}):wrappedItems;
return result;
};
// omitted methods for brevity
InspectionService = (function() {
var wrapDatabaseRecord = function(inspection) {
return {
getInspectionItemsWithResult: function(result) {
var items = [];
InspectionResults.find(
{ 'result': result, 'inspectionId': inspection._id })
.forEach(function(resultItem) {
items.push(JSON.parse(JSON.stringify(resultItem)));
items[items.length-1].key = resultItem.itemKey;
});
return items;
},
getEducationAssetsForItem: function(itemid) {
var report = inspection.report || {};
var item = report[itemid];
if (!item) {
item = { assets: [] };
}
var assets = item['assets'] || [];
return assets;
},
selectPhoto: function(name, selectionFlag) {
var update = false;
this.eachPhoto(function(photo) {
if (photo.photoName === name) {
photo.selected = selectionFlag;
update = true;
}
});
InspectionOrders.update({ _id: inspection._id }, {
$set: {
attachments: inspection.attachments
}
});
}
}
return {
findById: function(inspectionId) {
var inspection = InspectionOrders.findOne({ _id: inspectionId });
return inspection ? wrapDatabaseRecord(inspection) : null;
}
}
}());
<template name="assemble">
... some html
{{#if educationAssets}}
<div class="table-row">
<div class="table-cell">
Why it's important:<br>
<span style="font-size:70%; font-weight:normal;">Click to view</span>
</div>
<div class="table-cell">
{{#each educationAssets}}
<div class="relative education-asset">
<div
class="checkmark {{#if selected}}selected{{/if}}"
data-educationAsset="{{asset.key}}"
data-item="{{itemKey}}"></div>
<img
class="thumbnail {{#if selected}}selected{{/if}} view-animation"
data-item="{{itemKey}}"
data-assetkey="{{asset.key}}"
src="{{asset.thumbnail}}"/>
{{asset.name}}
</div>
{{/each}}
</div>
</div>
{{/if}}
... some html
<h2 id="additional-findings" class="section-heading">Items Needing Attention</h2>
{{#each items}}
<div class="additional-finding critical">
{{> detailedItem}}
</div>
{{/each}}
... some html
</template>
<template name="detailedItem">
display a detailed view of the finding
(HTML removed for brevity)
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment