【问题标题】:Meteor JS Shared Methods and ObjectsMeteor JS 共享方法和对象
【发布时间】:2015-12-04 07:22:08
【问题描述】:

我的问题

在使用 Meteor JS 时,我遇到了一些我不太确定如何修复的问题,因为所有内容似乎都以正确的顺序加载。它仍然没有以正确的方式行事,它说功能没有定义。

我查看了其他问题,其中大部分是指流星方法部分。我有点困惑,为什么我需要将它包装在 Method 对象中才能使用它们。

我的问题是如何在不使用流星方法调用并将它们作为子对象放入的情况下,将多个函数和“人造”类用作共享的 javascript 文件。

我正在尝试用作共享 js 的示例类。

function CustomerProjects(){
  // declare variables
  this.name = null;
  this.scope = null;
  this.time = null;
  this.completed = false; // set every project to default false
  this.description = null;

  // Get the time array
  this.billableHours  = function(){
    if(this.completed){
      var totalTime = 0; // int
      var time = this.time;
      // calculate time
      for(var i = 0; i < time.length; i++){
          totalTime = totalTime + time[i];
      }
      return totalTime;
    } else {
      return "Not Completed";
    }
  };
} 

我的文件结构

【问题讨论】:

标签: javascript meteor


【解决方案1】:

您的定义是文件范围的,将其更改为全局范围:

function CustomerProjects(){  // file scoped
..

CustomerProjects = function (){  // global scoped
..

【讨论】:

  • 官方感觉很蠢。感谢您的帮助和信息,这正是我所需要的。
猜你喜欢
  • 1970-01-01
  • 2011-11-24
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多