2016-02-25 61 views
-1

如果函數「neww」爲true,我試圖讓餘額上升,如果爲false,則下降。 neww是從0-1開始的一個隨機數:爲什麼我的餘額不會改變?

Template.result.helpers({ 
    'neww': function(){ 
    return(Session.get('number') > 0.5 ? true : false) 
    } 
}); 

所以這應該根據隨機生成的數字聲明新的真或假嗎?那麼,我有這樣一個if else語句:

Template.balance.events({ 
    'click button': function() { 
    if (neww = true) { 
     Session.set('bal', Session.get('bal') + 1); 
    } else { 
     Session.set('bal', Session.get('bal') - 1); 
    } 
    } 
}); 

它應該提高我的餘額1,如果數字大於.5,否則降低它。

我的整個代碼是:

if (Meteor.isClient) { 
    // counter starts at 0 
    Session.setDefault('number', Random.fraction()); 
    Session.setDefault('word', ""); 
    Session.setDefault('bal', 5000); 


    Template.hello.helpers({ 
    number: function() { 
     return Session.get('number'); 
    } 
    }); 

    Template.balance.helpers({ 
    bal: function() { 
     return Session.get('bal'); 
    } 
}); 
    Template.hello.helpers({ 
    word: function() { 
    return Session.get('word'); 
    } 
}); 

    Template.hello.events({ 
    'click button': function() { 
     // increment the counter when button is clicked 
     Session.set("number", 0+Random.fraction()); 
    } 

    }); 

Template.result.helpers({ 
    'neww': function(){ 
    return(Session.get('number') > 0.5 ? true : false) 
    } 
}); 

Template.balance.events({ 
    'click button': function() { 
    if (neww = true) { 
     Session.set('bal', Session.get('bal') + 1); 
    } else { 
     Session.set('bal', Session.get('bal') - 1); 
    } 
    } 
}); 

} 

if (Meteor.isServer) { 
    Meteor.startup(function() { 
    // code to run on server at startup 
    }); 
} 

任何幫助或建議,將不勝感激。

回答

0

neww與所有幫助者一樣,只能在您的模板本身中使用。如果你想在你的JS中使用它,只需使它成爲一個普通的JS函數並將其視爲正常。如果您想在模板中使用它,也可以將該函數分配給幫助程序。

目前,neww將在您嘗試使用它的上下文中未定義,所以當您單擊按鈕時,應該會在控制檯中看到錯誤。該函數在實際執行任何操作之前會拋出,這就是爲什麼平衡中沒有任何事情發生。

0
  1. 這並不是因爲如果

    如果(東西方婦女網絡=真)

  2. neww不變量,它是一個幫助正確的語法,所以你不能讓它在如果這樣的。爲了讓neww可在balance模板,你需要將它保存到一個全局變量像Session

我知道你是新的編碼,因此,瞭解基本的編程第一。與像meteor馬上框架的工作會讓你覺得無聊逐漸

+0

我應該從哪裏開始? –

+0

取決於你的背景。但是現在學習IT並不難,儀式呢? https://www.coursera.org/browse/computer-science?languages=en然後https://www.coursera.org/learn/meteor-development –

0

這裏是你如何修復代碼讓它做你想要什麼:

Template.balance.events({ 
    'click button': function() { 
    if (Session.get('number') > 0.5) { 
     Session.set('bal', Session.get('bal') + 1); 
    } else { 
     Session.set('bal', Session.get('bal') - 1); 
    } 
    } 
}); 

您可以完全擺脫你neww幫手。要詳細瞭解模板助手,事件和會話如何工作,請簽出Meteor Guide

相關問題