2013-05-06 49 views
2

我正在嘗試在我正在構建的應用程序中實現流星的.allow部分。在介紹它之前,一個列表顯示用戶輸入的評論,現在評論只是閃現一秒鐘然後消失。儘管如此,評論仍然被添加到集合中。 任何人都可以告訴我我做錯了什麼,我對此很新。流星js .allow列表不顯示

主要js文件:

if (Meteor.isClient) { 

    Meteor.startup(function() { 
    Meteor.subscribe("ques"); 
    }); 

    Template.compose.events({ 
    'submit form': function (event) { 
     var $body = $('#que-body'); 
     var $score = 1; 
     event.preventDefault(); 

    Questions.insert({ 
    body: $body.val(), 
    score: $score, 
    created_at: Date() 
    }); 

    $body.val(''); 
    } 
}); 

    Template.question.selected = function() { 
    return Session.equals("selected_question", this._id) ? "selected" : ''; 
    }; 

    Template.question.events({ 
    'click': function() { 
     Session.set("selected_question", this._id); 
    } 

    }); 

    Template.question.que = function(){ 
    return Questions.findOne(Session.get("selected")); 
    }; 

    // Deals with up-vote, down-vote, remove buttons 
    Template.list.events({ 
    'click .icon-thumbs-up': function(event) { 
     Questions.update(Session.get("selected_question"), {$inc: {score: 1}}); 
    }, 
    'click .icon-thumbs-down': function(event) { 
     Questions.update(Session.get("selected_question"), {$inc: {score: -1}}); 
    }, 
    'click .icon-remove': function(event) { 
     Questions.remove(Session.get("selected_question")); 
    } 
    }); 


    Template.list.questions = Questions.find({}, {sort: {score: -1, created_at: -1}}); 
} 

if (Meteor.isServer) { 
    Meteor.startup(function() { 
    Meteor.publish("ques", function(){ 
     return Questions.find({}, { 
      fields:{ } 
     }) 
    }); 
    }); 
} 

的model.js文件:

Questions = new Meteor.Collection("questions"); 

Questions.allow({ 

    insert: function(userId, que){ 
     return userId && que.owner === userId; 
    }, 

    update: function(id, ques, fields, modifier){ 
    return true; 
    }, 

    remove: function(id, que){ 
     return id && que.owner === id; 
    } 
}); 

回答

1

你的意思是問題(你說的意見嗎?):你Meteor.allow規則是基本的東西,說question.owner是當前登錄用戶的_id。插入問題時需要插入owner。這是唯一的方法(que.owner === userId將返回true):

Questions.insert({ 
    owner: Meteor.userId(), 
    body: $body.val(), 
    score: $score, 
    created_at: Date() 
}); 

請務必確保只有登錄用戶必須插入問題的機會。通過隱藏按鈕或在即將插入所有內容之前進行檢查:

if(!Meteor.userId()) { 
    alert("You need to be logged in to post a question"); 
    return; 
} 
+0

它的工作表示感謝,我添加了所有者屬性,它顯示列表並似乎正常工作。我在html中有一個聲明,只允許他們在登錄時插入問題。 – 2013-05-06 17:08:10

+0

只是想我會問在流星中是否有一種簡單的方法,可能與.allow一起使用,以便用戶可以提出問題一旦。 – 2013-05-06 20:11:36

+0

您可以在對客戶進行投票時將其用戶ID與投票相關聯。在meteor.allow中,你可以檢查它們的名字是否在包含投票的文檔中已經存在的數組中,如果不是,則返回true否則返回false – Akshat 2013-05-06 20:51:22