2014-10-02 14 views
0

,我創建了一個下拉菜單來顯示用戶鍵入的國家/地區列表。現在,他們可以選擇一個,點擊'sublit'然後通過流星調用插入到我的Posts集合中。下面的代碼以及這個jQuery代碼(保存在客戶端文件夾中)可以讓這個功能起作用。使用的輸入ID是「國家」。流星:允許預定值在我的表單中輸入集合

https://github.com/twitter/typeahead.js/blob/master/dist/typeahead.jquery.js 

我的目標:要允許在我的「國家」變量中列出的國家將被插入到數據庫中。我希望在我的方法函數中對其進行驗證。

CLIENT:

Template.createpost.rendered = function() { 
if (!this.rendered){ 
    var substringMatcher = function(strs) { 
return function findMatches(q, cb) { 
var matches, substrRegex; 
matches = []; 
substrRegex = new RegExp(q, 'i'); 
$.each(strs, function(i, str) { 
    if (substrRegex.test(str)) { 
    matches.push({ value: str }); 
    } 
}); 
cb(matches); 
}; 
}; 
var country = [ 
"Afghanistan", 
    "Albania", 
    "Algeria", 
    "Andorra", 
    "Angola", 
    "Antigua and Barbuda", 
    "Argentina", 
    "Rest Of Countries" 
]; 
$('#country').typeahead({ 
hint: true, 
highlight: true, 
minLength: 1 
}, 
{ 
name: 'country', 
displayKey: 'value', 
source: substringMatcher(country) 
}); 
this.rendered = true; 
} 
}; 


Template.createpost.events({ 
'submit form#createpost': function(e, tmpl) { 
    e.preventDefault(); 
    var insertPostData = { 
     country: $(e.target).find('#country').val() 
    } 
    Meteor.call('insertPostData', insertPostData, function(error, id){ 
     if (error) { 
      alert(error.reason); 
     } 
    }); 
} 

});

服務器:我的方法到目前爲止只會在用戶沒有選擇國家時拋出錯誤。我不確定使用語法來檢查從客戶端接收的國家/地區值與我希望在我的方法中列出的值的數組的關係我接受其他建議以實現此目的,請讓我知道。感謝你們。

Meteor.methods({ 
'insertPostData': function(insertPostData){ 
    if (!insertPostData.country) 
    throw new Meteor.Error(422, 'please select valid country'); 
    return insertPostData._id = AllPosts.insert(insertPostData); 

}});

+0

看看[Collection2](https://github.com/aldeed) /流星collection2)! – richsilv 2014-10-02 10:17:04

回答

0

這是我將繼續:首先,將您的國家/地區數組聲明爲應用程序的共享文件夾,以便它們在兩種環境中均可用。

lib/constants.js

countries=[...]; 

然後使用這個數組來建立你的預輸入客戶端和檢查對國家的有效性發送到方法。

if (!insertPostData.country || !_.contains(countries,insertPostData.country)){ 
    throw new Meteor.Error(422, 'please select valid country'); 
} 

我們使用_.contains來達到此目的。

http://underscorejs.org/#contains

+0

好的,這套驗證非常好。如何使用 Template.createpost.rendered代碼中填充的constants.js文件中的數組? (我現在在我的客戶端有underscore.js)。 – meteorBuzz 2014-10-02 10:17:34

+0

只需在您的客戶端代碼中使用名稱'countries'來引用它,即'source:substringMatcher(countries)'? – saimeunt 2014-10-02 10:29:27

+0

我在substringMatcher中引用了這個國家,但它沒有驗證。我從我提供的上述代碼中將其替換爲國家/地區。 – meteorBuzz 2014-10-02 10:46:56

0

如果它只是檢查,對數組中的值,我只用陣列上的濾波方法。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

var countries = [ 
"Afghanistan", 
    "Albania", 
    "Algeria", 
    "Andorra", 
    "Angola", 
    "Antigua and Barbuda", 
    "Argentina", 
    "Rest Of Countries" 
]; 

var insertPostData = {}; 

insertPostData.country = "Albania"; 

if(countries.filter(function(country){ return insertPostData.country === country; }).length > 0) 
    { 
     console.log("country listed"); 
    } else { 
     console.log('country not listed'); 
    } 

這種過濾貫穿陣列,並且創建一個基於你給它的過濾器的新數組,如果返回是真實的它增加了它,如果它沒有它跳過它。所以如果長度爲0,那麼它沒有找到任何符合該值的國家。

或者流星附帶下劃線庫,你可以這樣做:

Meteor.methods({ 
    'insertPostData': function(insertPostData){ 
    if (!insertPostData.country || _.contains(countries, insertPostData.country);) 
     throw new Meteor.Error(422, 'please select valid country'); 
    return insertPostData._id = AllPosts.insert(insertPostData); 
} }); 

其次添加在一個地方的陣列,客戶端和服務器可以看到像libs/countries.js

,並添加您的某處陣列沒有var關鍵字,因此它不限於該文件:

countries = [ 
    "Afghanistan", 
    "Albania", 
    "Algeria", 
    "Andorra", 
    "Angola", 
    "Antigua and Barbuda", 
    "Argentina", 
    "Rest Of Countries" 
]; 
+0

感謝您的意見傑夫,我會檢查出這種替代方法,但saimeunt更接近我現在需要的。 – meteorBuzz 2014-10-02 10:51:32

+0

嗨,傑夫,你可以請你擴展你給的選擇。 我只想檢查我的方法,即insertPostData.country等於在服務器端數組中列出的項目。 – meteorBuzz 2014-10-02 13:52:45