2017-01-04 1 views
2

내 프로젝트의 페이지에서 계산 된 속성을 얻으려고하고 있는데 아무 것도 작동하지 않는 것 같습니다. Firebase 데이터베이스에서 일부 값의 합계를 얻었습니다. 사용자 지정 속성 값으로 반환하려고합니다. 함수 자체가 작동하는 것을 알지만 반환 된 값을 표시하지는 않습니다. 내가 올바르게하고있는 것이 아니거나 내가 바꿀 수있는 것이 있습니까? 감사!계산 된 사용자 지정 속성이 폴리머의 페이지에 표시되지 않음

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

귀하의 계산 된 숙박 시설의 기능 : 값을 반환 할

<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> 

그리고 여기에있는 속성과 기능 : 값을합니다 (countedWords 속성을)를 표시하도록되어 어디 여기

입니다 실제로 아무것도 반환하지 않습니다. 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

나는 이것을 시도했지만 지금은 '단어'가 비어 있습니다. 이제 아무 것도 계산되지 않고 있습니다. 로깅 '단어'는 아무것도하지 않습니다. 그러나 'countedWords'는 페이지에 0으로 표시됩니다. – SyrupandSass

+0

위의 제안은 단어를 어떻게 계산하는지 변경하는 것을 포함하지 않습니다. 문제를 재현하는 바이올린/코덱이 있습니까? – tony19

+0

방금 ​​관찰자를 설정하지 않은'observers' 속성 이름 (오타가 잘못되었습니다)에서 오타가 나타났습니다. 왜 그것이 당신을 위해 작동하지 않았는 지 설명 할 수 있습니다. 답변을 업데이트했습니다. – tony19

관련 문제