【发布时间】:2018-11-09 03:24:38
【问题描述】:
我正在构建一个应用程序,用于自定义电子商务网站的产品网格顺序。能够以响应迅速且灵活的方式呈现产品订单,确实可以让项目经理更轻松地为他们的客户创造更好的体验。
此网格订单应用程序的一部分基于 Ryan Glover(我非常感谢他)的一篇文章,名为 Importing CSV's https://themeteorchef.com/tutorials/importing-csvs,使用 PapaParse。
尽管模板和构建是在 Meteor 中,但这有点像一个普通的 JavaScript 问题。
这是我非常精简的问题:如何将event object 放入template?我从Papa.parse() 函数中得到event object 很好,并且可以console.log(it)。但是,我如何将event object 传递到模板中。
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import './main.html';
// NOTE: trying to figure out how to pass event object to template
Template.upload.events({
'change [name="uploadCSV"]' ( event, template ) {
// Handles the conversion and upload
var fileInput = document.querySelector('[name="uploadCSV"]');
// Parse local CSV file
Papa.parse(fileInput.files[0], {
header: true,
complete: function(results) {
// console.log(results); // includes data, error, and misc
// NOTE: This is the data object to send to the template
let itemData = results.data;
console.log(itemData) // here is the data object
// This test correctly iterates over the object, but should be done in the template
itemData.forEach(function(item) {
console.log(item)
// console.log(item['itemcode'])
});
// HELP! How do I send the object itemData to the template?
} // END complete
}); // END parse
} // END change
}) // END events
这里是回购的链接。 https://github.com/dylannirvana/gridorderapp/tree/master/meteor/vanilla
这必须非常容易。提前谢谢了!
【问题讨论】:
-
您说
event对象,我假设您的意思是来自change事件的事件参数,但您的代码提到从解析结果中传递数据。哪个是正确的? -
无论哪种方式 - 您可以使用
Session或考虑将结果存储到您的数据库中以在同一模板中订阅(如果您想持久化它们) -
解析结果。我想将此事件数据传递给模板。我正在尝试与 reactiveVar 链接,但不断收到 typeError。由于某种原因,我也无法让数据库工作。
标签: javascript meteor meteor-blaze papaparse