Vue JS将增加的数量保存到Firebase数据库

时间:2017-04-05 22:54:33

标签: javascript firebase firebase-realtime-database vue.js vuefire

我有一个简单的VueJS应用程序,我正在使用Firebase JSON数据库。我希望能够单击箭头来更新评论的“投票”。 (类似于这个网站如何upvotes)。我的功能运行。但是Vue模型永远不会更新Firebase,因此刷新时会丢失投票计数。请参考我的CodePen:http://codepen.io/Auzy/pen/evowdd/(请注意我使用Pug和Stylus,查看普通的HTML / CSS,点击右上角的箭头。)

JS:

// Initialize Firebase
var config = {
    databaseURL: "..."
};
const firebaseapp = firebase.initializeApp(config);
const db = firebaseapp.database();
const commentsRef = db.ref('test');

const app = new Vue({
    el: '#app',
    data: {
        comments: [],
        newComment: {
            name: '',
            comment: '',
            votes: 0
        },
    },
    methods: {
        addComment() {
            commentsRef.push(this.newComment);
            this.newComment.name = '';
            this.newComment.message = '';
            this.newComment.votes = 0;
        },
        vote: function (voteType, comment) {
            if (voteType === "up") {
                comment.votes++
            } else if (voteType === "down") {
                if (comment.votes <= -10) {
                    return;
                }

                comment.votes--;
            }
        },
    },
    firebase: {
        comments: commentsRef
    },

})

1 个答案:

答案 0 :(得分:1)

好吧,我相信我已经弄明白了。如果你有更好的方法,请回答。

以下是添加了备注的新方法:

//...
for (size_t i = 0; i < n; ++i)
{
    std::string printStr;
    {
        std::stringstream sstr;
        sstr << i;
        printStr = sstr.str();
    }
    //now, you can use printStr
    //...
}