Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the standard pattern for ember-data validations? (invalid state, becameInvalid...)

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?

like image 451
josepjaume Avatar asked Jun 15 '12 16:06

josepjaume


1 Answers

I have a pending pull request that should fix this

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

like image 131
Cyril Fluck Avatar answered Oct 11 '22 09:10

Cyril Fluck