0

今すぐ更新私は自分のアプリでこれをやろうとしています(thx to Akshat

//一般

LANG = 'ru';
Dictionary = new Meteor.Collection("dictionary");

//if server
    Meteor.startup(function () {
    if (Dictionary.find().count() === 0) {
        // code to fill the Dictionary
    }
    });


    Meteor.publish('dictionary', function () {
        return Dictionary.find();
    });
//endif

//クライアント

t = function(text) {
    if (typeof Dictionary === 'undefined') return text;
    var res = Dictionary.find({o:text}).fetch()[0];
    return res && res.t;
}

    Meteor.subscribe('dictionary', function(){ 
      document.title = t('Let the game starts!'); 
    });

    Template.help.text = t('How to play');

//html

<body>
  {{> help}}
</body>


<template name="help">
    {{text}}
</template>

テンプレートがレンダリングされたとき、ディクショナリは定義されていませんでした。しかしt('How to play')、コンソールでは完全に動作します)

4

1 に答える 1

1

Javascript 変数は、クライアントとサーバーの間で事後的に共有されません。Meteor Collection を使用してデータを保存する必要があります。

if (Meteor.isServer) {

    var Dictionary = new Meteor.Collection("dictionary");

    if(Dictionary.find().count() == 0) {
    //If the 'dictionary collection is empty (count ==0) then add stuff in

        _.each(Assets.getText(LANG+".txt").split(/\r?\n/), function (line) {
            // Skip comment lines
            if (line.indexOf("//") !== 0) {
                var split = line.split(/ = /);
                DICTIONARY.insert({o: split[0], t:split[1]});
            }
        });
    }

}

if (Meteor.isClient) {

    var Dictionary = new Meteor.Collection("dictionary");

    Template.help.text = function() {
        return Dictionary.find({o:'Let the game starts!'});
    }
}

上記では、パッケージが含まれていると仮定していautopublishます(パッケージを作成するとデフォルトで含まれているため、これは特に気にする必要はありませんが、削除した場合に備えて)

ドキュメントのタイトルでは、わずかに異なる実装を使用する必要があります。これMeteor.startupは、html と javascript が最初にダウンロードされ、データが空であるため、実行時にデータがダウンロードされないことに注意してください。その後、反応的にデータを埋めます)

于 2013-10-02T13:45:24.283 に答える