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

前端 未结 5 744
囚心锁ツ
囚心锁ツ 2021-01-31 05:59

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 somewh

相关标签:
5条回答
  • 2021-01-31 06:06

    this may seem to be an overly simple answer, but why not create a new transaction and add the pre-existing record to it? i'm also trying to figure out an error handling approach.

    also you should probably consider writing this at the store level rather than the adapter level for the sake of re-use.

    0 讨论(0)
  • 2021-01-31 06:11

    I tried Javier's answer, but I get "Invalid Path" when doing any record.set(...) with the record in invalid state. What I found worked was:

    // with the record in invalid state
    record.send('becameValid');
    record.set('someProperty', 'a valid value');
    App.store.commit();
    

    Alternatively, it seems that if I call record.get(...) first then subsequent record.set(...) calls work. This is probably a bug. But the above work-around will work in general for being able to re-commit the same record even without changing any properties. (Of course, if the properties are still invalid it will just fail again.)

    0 讨论(0)
  • 2021-01-31 06:15

    I have a pending pull request that should fix this

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

    0 讨论(0)
  • 2021-01-31 06:22

    Give a look at this gist. Its the pattern that i use in my projects.

    https://gist.github.com/danielgatis/5550982

    0 讨论(0)
  • 2021-01-31 06:31

    For some unknown reason, the record becomes part of the store default transaction. This code works for me:

    var transaction = App.store.transaction();
    var record = transaction.createRecord(App.Post);
    record.set('someProperty', 'invalid value');
    transaction.commit()
    
    record.set('someProperty', 'a valid value');
    App.store.commit(); // The record is created in backend
    

    The problem is that after the first failure, you must always use the App.store.commit() with the problems it has.

    0 讨论(0)
提交回复
热议问题