定义了两个完全相同的流星本地集合和助手。一个帮助程序有效。另一个没有



我创建了这两个本地集合(代码实际上是一个接一个地编写的,就像下面一样):

ShoppingCartCollection = new Meteor.Collection(null);
CurrentPricesCollection = new Meteor.Collection(null);

Template.myTemplate.rendered中,我将一些初始信息添加到这些集合中(同样,代码是一个接一个):

ShoppingCartCollection.insert({"sqft" : "not yet entered"});
CurrentPricesCollection.insert({"hdrPhotos" : 100}); 

我在helpers.js中有这两个全局帮助器(一个接一个地定义)

Handlebars.registerHelper("shoppingCart", function() {
  return ShoppingCartCollection.findOne();
});
Handlebars.registerHelper("currentPrice", function() {
  return CurrentPricesCollection.findOne();
});

加载页面时,我立即在控制台中运行这些命令:

> ShoppingCartCollection.findOne();
Object {sqft: "not yet entered", _id: "xcNmqJvMqqD5j7wwn"}
> CurrentPricesCollection.findOne();
Object {hdrPhotos: 100, _id: "LP38E3MZgzuYjvSec"}

在我的模板中,我使用了这些帮助器,但是…

{{currentPrice.hdrPhotos}} //displays nothing
{{shoppingCart.sqft}} //displays "not yet entered"

如何……什么……? 这怎么可能呢?有没有什么我可能漏掉的陷阱?某种我不知道的依赖关系或加载顺序?

您发布的代码在这里工作良好。

建议将此代码与您正在执行的操作的确切细节进行比较。另外,看对于其他问题,拼写错误等

下面是我使用的确切测试过程:

在linux控制台中从无到有:

meteor create sodebug

注意,这将为"hello world"类型的程序生成文件。

查看版本:

meteor --version
Release 0.8.1.1
编辑sodebug/sodebug.js:

if (Meteor.isClient) {
  // code autogenerated by meteor create
  Template.hello.greeting = function () {
    return "Welcome to sodebug.";
  };
  Template.hello.events({
    'click input': function () {
      // template data, if any, is available in 'this'
      if (typeof console !== 'undefined')
        console.log("You pressed the button");
    }
  });
  // add your code here
    ShoppingCartCollection = new Meteor.Collection(null);
    CurrentPricesCollection = new Meteor.Collection(null);
    ShoppingCartCollection.insert({"sqft" : "not yet entered"});
    CurrentPricesCollection.insert({"hdrPhotos" : 100}); 
    Handlebars.registerHelper("shoppingCart", function() {
    return ShoppingCartCollection.findOne();
    });
    Handlebars.registerHelper("currentPrice", function() {
    return CurrentPricesCollection.findOne();
    });
}
if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}

编辑sodebug.html:

<head>
  <title>sodebug</title>
</head>
<body>
  {{> hello}}
  {{> T1 }}
  {{> T2 }} 
</body>
<template name="T1">
<p>
{{shoppingCart.sqft}}
</p>
</template>
<template name="T2">
<p>
{{currentPrice.hdrPhotos}}
</p>
</template>
<template name="hello">
  <h1>Hello World!</h1>
  {{greeting}}
  <input type="button" value="Click" />
</template>

Run: meteor run

手动测试:

在localhost:3000启动chromium浏览器

检查web浏览器控制台的集合数据。通过

检查网页浏览器屏幕上的模板数据。通过

sodebug.html文件中重新排序模板,检查web浏览器屏幕。通过

最新更新