2017-01-04 7 views
2

私のプロジェクトのページで計算されたプロパティを取得しようとしていますが、何も動作していないようです。 Firebaseデータベースからいくつかの値の合計を取得しています。カスタムプロパティの値として返すようにしています。私は、関数自体は動作しているが、返された値を表示していないことを知っている。私が正しくやっていないこと、あるいは私が変えることができることがありますか?ありがとう!計算されたカスタムプロパティがポリマーのページに表示されない

値は(countedWordsプロパティ)を示すことになっているのはここです:

<h1>[[theProject.title]]</h1> 
    <p class="lead">[[theProject.author]]</p> 
    <p><span>[[theProject.category]]</span> <span>[[theProject.genre]]</span></p> 
    <p>Word Count: <span>{{countedWords}}</span> of <span>[[theProject.goal]]</span></p> 

そしてここでは、値を返すプロパティと機能である:

properties: { 
    ... 
    countedWords: { 
    type: String, 
    computed: '_countedWords(pattern)' 
    } 
}, 

_countedWords: function(pattern) { 
    // var pathname = window.location.pathname; 
    // var project = pathname.substring(pathname.lastIndexOf('/')); 
    // var projId = project.substring(1); 
    var total = 0; 
    var keys = []; 
    var counts =[]; 

    var ref = firebase.database().ref().child('/scenes/' + pattern).orderByChild('wordcount'); 
    ref.once('value', function(snap) { 
    snap.forEach(function(item) { 
     var itemVal = item.val(); 
     keys.push(itemVal); 
    }); 

    for (i=0; i < keys.length; i++) { 
     counts.push(keys[i].wordcount); 
    } 

    function getSum(total, num) { 
     return total + num; 
    } 

    total = counts.reduce(getSum); 
    words = total.toString(); 
    console.log(words); 
    return words; 
    }); 
}, 

答えて

1

あなたの計算されたプロパティの機能実際に何かを返すわけではありません。 return wordsステートメントは、_countedWords()の代わりにref.once()(実際には効果がない)へのコールバックに値を返します。 ref.once()の結果は非同期なので、計算されたプロパティからthis.countedWordsを設定するpatternのオブザーバに切り替える必要があります。

Polymer({ 
    properties: { 
    pattern: String, 
    countedWords: { 
     type: Number, 
     value: 0 
    } 
    }, 

    observers: ['_countedWords(pattern)'], 

    _countedWords: function(pattern) { 
    // ... 
    ref.once('value', function(snap) { 
     // ... 
     this.countedWords = words; 
    }.bind(this)); 
    }, 

    // ... 
}); 
+0

私はこれを試しましたが、今は「単語」が空です。今は何もカウントされていません。ロギング 'words'は何もしません。しかし、 'countedWords'はページに0として表示されます。 – SyrupandSass

+0

上記の提案には、単語の計算方法の変更は含まれていません。問題を再現するフィーリング/コードブックがありますか? – tony19

+0

私は 'observers'プロパティ名のtypoに気づきました(それは間違って' observer'でした)。これは意図したオブザーバを設定しませんでした。それはなぜそれがあなたのために働かなかったか説明するかもしれない。私は答えを更新しました。 – tony19

関連する問題