删除Discover Meteor Book中的URL字段

时间:2022-09-29 19:41:56

I'm trying to delete the "URL" part in the Discover Meteor Book so that it is more of a chat room. I deleted and changed the post_submit.js so that the error statement return is true. When I goto the "Submit Post" and only submit in the "Text" field and no "URL", it won't submit.

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

Do I need to change anything in the post_item.js or posts.js in the lib/collections?

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

Most of this issue seems to be in the Chapter 7 part when the method is introduced.

当引入该方法时,大多数问题似乎都在第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;
    }
});

Does this.url do anyting?

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

url is in here too..

网址也在这里..

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>

1 个解决方案

#1


You should remove all references to the url if you don't want it.

如果不需要,您应该删除对该URL的所有引用。

Check out the project: git clone https://github.com/DiscoverMeteor/Microscope.git

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

Look for all files containing "url": grep -i -r url *

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

Results:

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,

Those are the files the need to be modified.

这些是需要修改的文件。

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

fixtures.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
    });
  }
}

#1


You should remove all references to the url if you don't want it.

如果不需要,您应该删除对该URL的所有引用。

Check out the project: git clone https://github.com/DiscoverMeteor/Microscope.git

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

Look for all files containing "url": grep -i -r url *

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

Results:

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,

Those are the files the need to be modified.

这些是需要修改的文件。

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

fixtures.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
    });
  }
}