ember数据验证的标准模式是什么? (无效状态,成为无效...) [英] What's the standard pattern for ember-data validations? (invalid state, becameInvalid...)

查看:119
本文介绍了ember数据验证的标准模式是什么? (无效状态,成为无效...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段时间一直在努力,让我们看看有人可以帮助我。



虽然在自述文件中没有明确说明,但是ember数据提供了一些验证支持。您可以在代码和文档的某些部分看到:



https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/model /states.js#L411



https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/model/states。 js#L529



REST适配器本身不添加验证支持,但是我发现如果我在ajax调用中添加了这样的东西,我可以把模型放在一个无效的状态,并从服务器端出来的错误对象:

  error:function xhr){
var data = Ember。$。parseJSON(xhr.responseText);
store.recordWasInvalid(record,data.errors);
}

所以我可以很容易地执行以下操作:

  var transaction = App.store.transaction(); 
var record = transaction.createRecord(App.Post);
record.set('someProperty','invalid value');
transaction.commit()
//这使得验证失败

record.set('someProperty','a valid value');
transaction.commit();
//这不会再次触发提交。

事情是:如你所见,交易不尝试重新承诺这里此处



所以事情是:如果我不能重用提交,我该怎么处理这个?我有点怀疑我有异议的模式将无效状态 - 由阅读文档,这似乎是为客户端验证。在这种情况下,我应该如何使用它们?

解决方案

我有一个待解决的请求,应该修复这个



https://github.com/emberjs/data/pull / 539


I've kinda been struggling with this for some time; let's see if somebody can help me out.

Although it's not explicitly said in the Readme, ember-data provides somewhat validations support. You can see that on some parts of the code and documentation:

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/model/states.js#L411

https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/model/states.js#L529

The REST adapter doesn't add validations support on itself, but I found out that if I add something like this in the ajax calls, I can put the model on a "invalid" state with the errors object that came from the server side:

error: function(xhr){
  var data = Ember.$.parseJSON(xhr.responseText);
  store.recordWasInvalid(record, data.errors);
}

So I can easily to the following:

var transaction = App.store.transaction();
var record = transaction.createRecord(App.Post);
record.set('someProperty', 'invalid value');
transaction.commit()
// This makes the validation fail

record.set('someProperty', 'a valid value');
transaction.commit();
// This doesn't trigger the commit again.

The thing is: As you see, transactions don't try to recommit. This is explained here and here.

So the thing is: If I can't reuse a commit, how should I handle this? I kinda suspect that has something to do to the fact I'm asyncronously putting the model to the invalid state - by reading the documentation, it seems like is something meant for client-side validations. In this case, how should I use them?

解决方案

I have a pending pull request that should fix this

https://github.com/emberjs/data/pull/539

这篇关于ember数据验证的标准模式是什么? (无效状态,成为无效...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆