gpt4 book ai didi

javascript - 删除 Discover Meteor Book 中的 URL 字段

转载 作者:行者123 更新时间:2023-12-03 10:09:32 25 4
gpt4 key购买 nike

我正在尝试删除《Discover Meteor Book》中的“URL”部分,以便它更像是一个聊天室。我删除并更改了post_submit.js,以便错误语句返回为true。当我转到“提交帖子”并仅在“文本”字段中提交而没有“URL”时,它不会提交。

我需要更改 lib/collections 中 post_item.js 或 posts.js 中的任何内容吗?

这个问题大部分好像是在第7章介绍方法的部分。

post_submit.js

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()
};

Meteor.call('postInsert', post, function(error, result) {
// display the error to the user and abort
if (error)
return true;

// show this result but route anyway
if (result.postExists)
return true;

Router.go('postsList', {_id: result._id});
});
}
});

post_item.js

Template.postItem.helpers({
ownPost: function() {
return this.userId === Meteor.userId();
},
domain: function() {
var a = document.createElement('a');
a.href = this.url;
return a.hostname;
}
});

this.url 有什么作用吗?

posts.js

Posts = new Mongo.Collection('posts');

Meteor.methods({
postInsert: function(postAttributes) {
check(this.userId, String);
check(postAttributes, {
title: String,
url: String
});

var postWithSameLink = Posts.findOne({url: postAttributes.url});
if (postWithSameLink) {
return {
postExists: true,
_id: postWithSameLink._id
}
}

var user = Meteor.user();
var post = _.extend(postAttributes, {
userId: user._id,
author: user.username,
submitted: new Date()
});

var postId = Posts.insert(post);

return {
_id: postId
};
}
});

网址也在这里..

post_submit.html

<template name="postSubmit">
<form class="main form page">

<div class="form-group">
<label class="control-label" for="title">Title</label>
<div class="controls">
<input name="title" id="title" type="text" value="" placeholder="Name your post" class="form-control"/>
</div>
</div>
<input type="submit" value="Submit" class="btn btn-primary"/>
</form>
</template>

最佳答案

如果您不想要该网址,则应删除所有对该网址的引用。

查看项目:git clone https://github.com/DiscoverMeteor/Microscope.git

查找所有包含“url”的文件:grep -i -r url *

结果:

client/templates/posts/post_item.html:      <h3><a href="{{url}}">{{title}}</a><span>{{domain}}</span></h3>
client/templates/posts/post_submit.js: url: $(e.target).find('[name=url]').val(),
client/templates/posts/post_submit.js: if (errors.title || errors.url)
client/templates/posts/post_submit.html: <div class="form-group {{errorClass 'url'}}">
client/templates/posts/post_submit.html: <label class="control-label" for="url">URL</label>
client/templates/posts/post_submit.html: <input name="url" id="url" type="text" value="" placeholder="Your URL" class="form-control"/>
client/templates/posts/post_submit.html: <span class="help-block">{{errorMessage 'url'}}</span>
client/templates/posts/post_edit.js: url: $(e.target).find('[name=url]').val(),
client/templates/posts/post_edit.js: if (errors.title || errors.url)
client/templates/posts/post_edit.html: <div class="form-group {{errorClass 'url'}}">
client/templates/posts/post_edit.html: <label class="control-label" for="url">URL</label>
client/templates/posts/post_edit.html: <input name="url" id="url" type="text" value="{{url}}" placeholder="Your URL" class="form-control"/>
client/templates/posts/post_edit.html: <span class="help-block">{{errorMessage 'url'}}</span>
client/templates/posts/post_item.js: a.href = this.url;
lib/collections/posts.js: return (_.without(fieldNames, 'url', 'title').length > 0);
lib/collections/posts.js: return errors.title || errors.url;
lib/collections/posts.js: if (!post.url)
lib/collections/posts.js: errors.url = "Please fill in a URL";
lib/collections/posts.js: url: String
lib/collections/posts.js: if (errors.title || errors.url)
lib/collections/posts.js: throw new Meteor.Error('invalid-post', "You must set a title and URL for your post");
lib/collections/posts.js: var postWithSameLink = Posts.findOne({url: postAttributes.url});
server/fixtures.js: url: 'http://sachagreif.com/introducing-telescope/',
server/fixtures.js: url: 'http://meteor.com',
server/fixtures.js: url: 'http://themeteorbook.com',
server/fixtures.js: url: 'http://google.com/?q=test-' + i,

这些是需要修改的文件。

post_item.html:

<template name="postItem">
<div class="post">
<a href="#" class="upvote btn btn-default {{upvotedClass}}">upvote</a>
<div class="post-content">
<h3>{{title}}</h3>
<p>
{{pluralize votes "Vote"}},
submitted by {{author}},
<a href="{{pathFor 'postPage'}}">{{pluralize commentsCount "comment"}}</a>
{{#if ownPost}}<a href="{{pathFor 'postEdit'}}">Edit</a>{{/if}}
</p>
</div>
<a href="{{pathFor 'postPage'}}" class="discuss btn btn-default">Discuss</a>
</div>
</template>

post_submit.js:

Template.postSubmit.onCreated(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 = {
title: $(e.target).find('[name=title]').val()
};

var errors = validatePost(post);
if (errors.title)
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);

Router.go('postPage', {_id: result._id});
});
}
});

post_submit.html:

<template name="postSubmit">
<form class="main form page">
<div class="form-group {{errorClass 'title'}}">
<label class="control-label" for="title">Title</label>
<div class="controls">
<input name="title" id="title" type="text" value="" placeholder="Name your post" class="form-control"/>
<span class="help-block">{{errorMessage 'title'}}</span>
</div>
</div>
<input type="submit" value="Submit" class="btn btn-primary"/>
</form>
</template>

post_edit.js:

Template.postEdit.onCreated(function() {
Session.set('postEditErrors', {});
});

Template.postEdit.helpers({
errorMessage: function(field) {
return Session.get('postEditErrors')[field];
},
errorClass: function (field) {
return !!Session.get('postEditErrors')[field] ? 'has-error' : '';
}
});

Template.postEdit.events({
'submit form': function(e) {
e.preventDefault();

var currentPostId = this._id;

var postProperties = {
title: $(e.target).find('[name=title]').val()
}

var errors = validatePost(postProperties);
if (errors.title)
return Session.set('postEditErrors', errors);

Posts.update(currentPostId, {$set: postProperties}, function(error) {
if (error) {
// display the error to the user
throwError(error.reason);
} else {
Router.go('postPage', {_id: currentPostId});
}
});
},

'click .delete': function(e) {
e.preventDefault();

if (confirm("Delete this post?")) {
var currentPostId = this._id;
Posts.remove(currentPostId);
Router.go('home');
}
}
});

post_edit.html:

<template name="postEdit">
<form class="main form page">
<div class="form-group {{errorClass 'title'}}">
<label class="control-label" for="title">Title</label>
<div class="controls">
<input name="title" id="title" type="text" value="{{title}}" placeholder="Name your post" class="form-control"/>
<span class="help-block">{{errorMessage 'title'}}</span>
</div>
</div>
<input type="submit" value="Submit" class="btn btn-primary submit"/>
<hr/>
<a class="btn btn-danger delete" href="#">Delete post</a>
</form>
</template>

post_item.js:

Template.postItem.helpers({
ownPost: function() {
return this.userId == Meteor.userId();
},
upvotedClass: function() {
var userId = Meteor.userId();
if (userId && !_.include(this.upvoters, userId)) {
return 'btn-primary upvotable';
} else {
return 'disabled';
}
}
});

Template.postItem.events({
'click .upvotable': function(e) {
e.preventDefault();
Meteor.call('upvote', this._id);
}
});

posts.js:

Posts = new Mongo.Collection('posts');

Posts.allow({
update: function(userId, post) { return ownsDocument(userId, post); },
remove: function(userId, post) { return ownsDocument(userId, post); },
});

Posts.deny({
update: function(userId, post, fieldNames) {
// may only edit the following fields:
return (_.without(fieldNames, 'title').length > 0);
}
});

Posts.deny({
update: function(userId, post, fieldNames, modifier) {
var errors = validatePost(modifier.$set);
return errors.title;
}
});

validatePost = function (post) {
var errors = {};

if (!post.title)
errors.title = "Please fill in a headline";

return errors;
}

Meteor.methods({
postInsert: function(postAttributes) {
check(this.userId, String);
check(postAttributes, {
title: String
});

var errors = validatePost(postAttributes);
if (errors.title)
throw new Meteor.Error('invalid-post', "You must set a title for your post");

var user = Meteor.user();
var post = _.extend(postAttributes, {
userId: user._id,
author: user.username,
submitted: new Date(),
commentsCount: 0,
upvoters: [],
votes: 0
});

var postId = Posts.insert(post);

return {
_id: postId
};
},

upvote: function(postId) {
check(this.userId, String);
check(postId, String);

var affected = Posts.update({
_id: postId,
upvoters: {$ne: this.userId}
}, {
$addToSet: {upvoters: this.userId},
$inc: {votes: 1}
});

if (! affected)
throw new Meteor.Error('invalid', "You weren't able to upvote that post");
}
});

fixture.js:

// Fixture data
if (Posts.find().count() === 0) {
var now = new Date().getTime();

// create two users
var tomId = Meteor.users.insert({
profile: { name: 'Tom Coleman' }
});
var tom = Meteor.users.findOne(tomId);
var sachaId = Meteor.users.insert({
profile: { name: 'Sacha Greif' }
});
var sacha = Meteor.users.findOne(sachaId);

var telescopeId = Posts.insert({
title: 'Introducing Telescope',
userId: sacha._id,
author: sacha.profile.name,
submitted: new Date(now - 7 * 3600 * 1000),
commentsCount: 2,
upvoters: [], votes: 0
});

Comments.insert({
postId: telescopeId,
userId: tom._id,
author: tom.profile.name,
submitted: new Date(now - 5 * 3600 * 1000),
body: 'Interesting project Sacha, can I get involved?'
});

Comments.insert({
postId: telescopeId,
userId: sacha._id,
author: sacha.profile.name,
submitted: new Date(now - 3 * 3600 * 1000),
body: 'You sure can Tom!'
});

Posts.insert({
title: 'Meteor',
userId: tom._id,
author: tom.profile.name,
submitted: new Date(now - 10 * 3600 * 1000),
commentsCount: 0,
upvoters: [], votes: 0
});

Posts.insert({
title: 'The Meteor Book',
userId: tom._id,
author: tom.profile.name,
submitted: new Date(now - 12 * 3600 * 1000),
commentsCount: 0,
upvoters: [], votes: 0
});

for (var i = 0; i < 10; i++) {
Posts.insert({
title: 'Test post #' + i,
author: sacha.profile.name,
userId: sacha._id,
submitted: new Date(now - i * 3600 * 1000 + 1),
commentsCount: 0,
upvoters: [], votes: 0
});
}
}

关于javascript - 删除 Discover Meteor Book 中的 URL 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30198019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com