简体   繁体   English

在Rails验证失败(422错误)后,如何从ember.js前端清除新记录?

[英]How to clear a new record from ember.js frontend after it fails Rails validation (422 error)?

I'm working on a basic reddit clone app with Rails and ember.js (via the ember-rails gem). 我正在使用Rails和ember.js开发一个基本的reddit克隆应用程序(通过ember-rails gem)。 Basically I have a 'post' model/controller in Rails which works correctly, but when I add a new post from the ember post model's create action, even if it fails the Rails validation, if I then go to the 'posts' index page which lists all the posts, I can see it there (ie ember is keeping the data). 基本上,我在Rails中有一个“发布”模型/控制器,它可以正常工作,但是当我从余烬发布模型的create操作添加新发布时,即使它未通过Rails验证,也可以转到“发布”索引页面其中列出了所有帖子,我在那里可以看到它(即,炭烬保留了数据)。 When I refresh it goes away, but I'm wondering what is the best way to purge that data so that it gets deleted upon rejection from the backend? 当我刷新时,它消失了,但是我想知道清除数据的最佳方法是什么,以使它在被后端拒绝时被删除? Another odd thing is that simply going to the posts/new page at all creates a new blank post which is then visible on the Then on the client-side, I have the following files in app/assets/javascripts/routes: 另一个奇怪的事情是,只需转到帖子/新页面就会创建一个新的空白帖子,然后在“空白”中可见。然后在客户端,我在app / assets / javascripts / routes中具有以下文件:

posts_route.js : posts_route.js

RedditJp.PostsRoute = Ember.Route.extend({

  model: function() {
      return this.get('store').find('post');
  }
});

posts_new_route.js: posts_new_route.js:

RedditJp.PostsNewRoute = Ember.Route.extend({

  model: function(){
    return this.get('store').createRecord('post'); },

  actions: {
    create: function() {
      var newPost = this.get('currentModel');
      var self = this;
      newPost.save().then(
        function() {  self.transitionTo('posts'); },
        function() { }
        );
    }
  }
});

Here's the form I'm trying to use to submit the data in posts/new.hbs: 这是我试图用来在posts / new.hbs中提交数据的表格:

<h1> Add a post </h1>
{{#each error in errors.title}}
  <p>{{error.message}}</p>
{{/each}}

<form {{action "create" on="submit"}}>
  <div>
    <label>
      Title<br/>
      {{input type="text" value=title}}
    </label>
  </div>
  <div>
    <label>
      Address<br/>
      {{input type="text" value=address}}
    </label>
  </div>
  <div>
    <label>
      Vote count<br/>
      {{input type="text" value=voteCount}}
    </label>
  </div>
  <button>Save</button>
</form>

and then in assets/javascripts/templates/posts/ I have index.hbs: 然后在assets / javascripts / templates / posts /中,我有index.hbs:

<h1>Posts</h1>
<ul>
  {{#each}}
      <li>{{title}} at {{address}} vote count: {{voteCount}}</li>
  {{else}}
      <li>There are no posts.</li>
  {{/each}}
</ul>

and here's my router.js: 这是我的router.js:

RedditJp.Router.map(function() {
  this.resource('posts', function() {
    this.route('new')
    });

  this.resource('home', function() {
    });

});

RedditJp.IndexRoute = Ember.Route.extend({
  redirect: function(){
    this.transitionTo('home')
    }
  });

I was thinking I could just add a check in the posts/index.hbs file and only show records that aren't dirty, but there must be a cleaner way of doing it, so I'm wondering what would be considered best practice in this case (I'm thinking there should be some code I could add to the promise in posts_new_route.js to deal with this, but I can't quite figure it out). 我当时想我可以只在posts / index.hbs文件中添加一个检查,只显示不脏的记录,但是必须有一种更清洁的方法,所以我想知道在什么情况下最好的做法是这种情况(我想应该在posts_new_route.js中添加一些可以解决这个问题的代码,但是我不太清楚)。

Thanks a lot! 非常感谢! And let me know if you need any additional info. 并告诉我您是否需要任何其他信息。

You can check if model isNew in template to hide new Record ( also you can use isEmpty property ) 您可以检查模板中的模型isNew是否隐藏新记录(也可以使用isEmpty属性)

    var record = store.createRecord('model');
record.get('isNew'); // true

record.save().then(function(model) {
  model.get('isNew'); // false
});

In template will look like {{each model}} {{#if model.get('isNew')}} 模板中的外观类似于{{each model}} {{#if model.get('isNew')}}

record.save().then(function(){ // Success callback }, function() { model..deleteRecord(); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM