我一直在試圖自己想象這個,但最終無法在很長時間過後。發現流星:將http://自動添加到輸入的URL
我目前正在關注Meteor.js的Discover Meteor Book。
我注意到提交沒有http://
的帖子會鏈接到localhost:3000/submittedurl
。
我想讓流星在提交時自動將http://
添加到網址。從邏輯上講,當方案包含在輸入字段中時,它不會添加http://
。
//post_submit.js
Template.postSubmit.created = function() {
Session.set('postSubmitErrors', {});
};
Template.postSubmit.helpers({
errorMessage: function(field) {
return Session.get('postSubmitErrors')[field];
},
errorClass: function(field) {
return !!Session.get('postSubmitErrors')[field] ? 'has-error' : '';
}
});
Template.postSubmit.events({
'submit form': function(e) {
e.preventDefault();
var post = {
url: $(e.target).find('[name=url]').val(),
title: $(e.target).find('[name=title]').val()
};
var errors = validatePost(post);
if (errors.title || errors.url)
return Session.set('postSubmitErrors', errors);
Meteor.call('postInsert', post, function(error, result) {
// display the error to the user and abort
if (error)
return throwError(error.reason);
// show this result but route anyway
if (result.postExists)
throwError('This link has already been posted');
Router.go('postPage', {_id: result._id});
});
}
});
請準確,並解釋更多關於問題 – ajduke 2014-11-20 17:51:54