如何正确编写Meteor模板助手

时间:2016-05-28 01:28:38

标签: javascript meteor

我不确定我是否理解(函数名称周围的引号)之间的区别:

Template.year.helpers({
  'QueryHeader': function() {
    return Session.get('keyQueryHeader') + ' Attendees';
  }
});

Template.year.helpers({
  QueryHeader: function() {
    return Session.get('keyQueryHeader') + ' Attendees';
  }
});

我的IDE提到一些关于从构造函数返回的原始值的东西,如果使用" new"如果我不使用函数名称周围的引号。如果我使用引号,它不会给我警告。编写助手的两种方式似乎都应该做到。我应该在名字周围使用引号吗?

2 个答案:

答案 0 :(得分:1)

使用引号时,您可以使用包含特殊字符,空格等的名称:

USE YourDatabase
GO
SET STATISTICS IO, TIME ON;
GO
-- A table must have a primary key which implicity will create the clustered index
CREATE CLUSTERED INDEX CLIX_[Table]_YourPrimaryKey_Or_RowIdentifier ON [Table] (YourPrimaryKey_Or_RowIdentifier)
GO
-- A nonclustered index is needed if we are going to retrieve information using the Variable Column
CREATE NONCLUSTERED INDEX IX_Table_Variable ON [Table] (Variable)
GO
Select * FROM [dbo].[Table] where [Variable] = 'NN12345'
GO
--Check Results, copy the values from the messages tab to http://www.statisticsparser.com/
GO
Select [Variable] FROM [dbo].[Table] where [Variable] = 'NN12345'
GO
--Check Results, copy the values from the messages tab to http://www.statisticsparser.com/
--Compare the values for logical reads on the two executions
GO
--if you need more columns for the select statement, you can add them as included columns in this way:
GO
DROP INDEX IX_Table_Variable
GO
CREATE NONCLUSTERED INDEX IX_Table_Variable ON [Table] (Variable) INCLUDE (TheOthersColumns)
GO
--PLEASE make sure you are testing on the development environment prior to move to PROD.

虽然这会抛出Uncaught SyntaxError:Unexpected identifier

var a = {
  'hello world': 'works!'
}

请参阅What characters are valid for JavaScript variable names?

你的IDE提到构造函数的原因是因为有一个关于用大写首字母编写构造函数的约定:

var a = {
  hello world': 'breaks'
}

答案 1 :(得分:1)

感兴趣的助手QueryHeader返回Session.get('keyQueryHeader') + ' Attendees',这是最有可能的类型字符串的primitive值。从函数返回一个字符串绝对没问题。但是,该功能以大写字母“Q&C”开头。当IDE没有被引号括起时,IDE会将该函数视为类构造函数。在这种情况下,构造函数的返回值是创建的实例,无论具体返回什么原始值。为此,您的IDE会发出警告primitive value returned from constructor will be lost

简单地说,我相信以下代码段符合IDE的行为。

// This gives the warning.
var s = {
    S: function() { return 'some string'; }
};

// These don't.
var s = {
    'S': function() { return 'some string'; }
};

var s = {
    S: function() { return {}; }
};

var s = {
    'S': function() { return {}; }
};