2013-11-22 2 views
0

fruit은 템플릿 도우미 Template.fruits.nameTemplate.fruits.nick간에 공유되는 변수로 각 개별 도우미 함수에서 코드가 중복되지 않도록합니다.Session.get은 Meteor.js에서 변수를 설정하지 않습니다.

change #fruit-selector 이벤트 핸들러가 트리거 될 때 이 설정하더라도 공유 변수 fruit은 변경되지 않습니다.

내 말은 fruit이라는 변수를 사용할 수 없다는 뜻입니까?

main.js

(function() { 

    // Set default fruit 
    if(!Session.get('fruit')) { 
     Session.set('fruit', 'apple'); 
    } 

    var fruit = Session.get('fruit'); 


    Template.fruits.name = function() { 
     return fruit; 
    }; 

    Template.fruits.nickname = function() { 
     return fruit + 'y'; 
    }; 

    Template.fruits.name2 = function() { 
     return Session.get('fruit'); 
    }; 


    Template.fruits.events({ 
     'change #fruit-selector': function(e) { 
      Session.set('fruit', e.target.value); 
      console.log('fruit: ' + fruit) 
     } 
    }); 

}()); 

답변

1

당신은 그렇지 않으면 fruit 반응성 데이터 소스 아니다 (변경에 응답하지 않습니다, 템플릿 헬퍼 내부 Session.get('fruit') 사용해야합니다, 그것은에서 그것을 값을 가지고있다 무 반응 데이터 소스).

PS 접근 방식 대신 Session.setDefault('fruit', 'apple')을 사용하십시오.

0

이을 (당신은 Template.fruit.nickname 또는 Template.fruits.nickname을 의미 했습니까?) 시도해보십시오, 세션이 기본 유성 패키지의 더 이상 일부가 아닙니다처럼

(function() { 

    // Set default fruit 
    if(!Session.get('fruit')) { 
     Session.set('fruit', 'apple'); 
    } 

    Template.fruits.name = function() { 
     return Session.get('fruit'); 
    }; 

    Template.fruit.nickname = function() { 
     return Session.get('fruit') + 'y'; 
    }; 

    Template.fruits.name2 = function() { 
     return Session.get('fruit'); 
    }; 


    Template.fruits.events({ 
     'change #fruit-selector': function(e) { 
      Session.set('fruit', e.target.value); 
      console.log('fruit: ' + Session.get('fruit')) 
     } 
    }); 

}()); 
0

보인다. 명령 프롬프트로 이동하여 "유성 추가 세션"을 실행하십시오.

관련 문제