2017-09-22 45 views
1

我一直在嘗試用Coffeescript和玉流星。而對於最基本的應用程序,我寫了下面的代碼。使用流星與咖啡和玉:callback [i] .call不是函數

main.coffee

import './hello.coffee' 

import './main.jade' 

main.jade

head 
    title Chatter 

body 
    h1 Welcome to Chatter! 
    +hello 

hello.coffee

import { Template } from 'meteor/templating' 
import { ReactiveVar } from 'meteor/reactive-var' 

import './hello.jade' 

Template.hello.onCreated 
    helloOnCreated: -> 
     @counter = new ReactiveVar 0 
     return 

Template.hello.helpers 
    counter: -> Template.instance().counter.get() 

Template.hello.events 
    'click button': (event, instance) -> 
     instance.counter.set instance.counter.get() + 1 
     return 

hello.jade

template(name="hello") 
    button Click me! 
    p You have pressed the button #{counter} times. 

現在,當我試圖運行這個應用程序,我得到這個錯誤Uncaught TypeError: callbacks[i].call is not a function。我對此很新,所以任何幫助將不勝感激。謝謝!

回答

2

您當前正通過Template.hello.onCreated的某個對象具有helloOnCreated屬性。只需直接通過Template.hello.onCreated一個函數即可。

Template.hello.onCreated -> 
    @counter = new ReactiveVar 0 
    return 

Meteor's documentation,所述onCreatedonRenderedonDestroyed屬性接受的功能。

eventshelpers屬性接受對象,就像你有。

+0

謝謝您的回答 – smashingpumpkin

+0

不是沒有勝! 如果您第一次嘗試使用Meteor,我會建議您使用Meteor的內置Blaze模板引擎和標準Javascript來做到這一點。我知道你可以使用Jade和Coffeescript,但你可以從他們的標準開始,更好地感受Meteor的機制(並且有更多的示例代碼)。只是一個建議。在你的閒暇時忽略。 :) –

+0

是的,我跟着Blaze的官方Todos教程。我自己製作了一個應用程序,所以我想要試用CoffeeScript。感謝您的建議和幫助。 :) – smashingpumpkin