简体   繁体   English

Ember.JS最佳实践:模型与控制器字段绑定

[英]Ember.JS Best Practice: Model vs Controller field binding

I am looking best practices in Ember.JS to fix the following scenario: 我正在Ember.JS中寻找最佳实践来修复以下情况:

  1. The user clicks on the challenges link. 用户单击挑战链接。
  2. The system displays the list of challenges. 系统显示挑战列表。
  3. The user clicks on edit challenge button. 用户单击编辑挑战按钮。
  4. The system displays the edit challenge form. 系统显示编辑质询表格。
  5. The user updates the challenge name. 用户更新质询名称。
  6. The user clicks on the leagues link without saving the challenge. 用户单击联赛链接而不保存挑战。
  7. The system displays the list of leagues. 系统显示联赛列表。
  8. The user clicks on the challenges link. 用户单击挑战链接。
  9. The system displays the list of challenges with the updated challenge name. 系统显示具有更新的挑战名称的挑战列表。

This issue is occurring because all my text field are binded directly to the challenge model and since the model is updated as soon as you type it updates the text on all the routes. 发生此问题的原因是,我的所有文本字段都直接绑定到质询模型,并且由于在您键入模型后即会对其进行更新,因此会更新所有路线上的文本。 I have a cancel button on the edit form where I do this.get('model').rollback() on the model to cancel out the edits. 我在编辑表单上有一个取消按钮,我在模型上执行this.get('model')。rollback()以取消编辑。 However this gets messy if you start doing rollback in different place on the page you can click. 但是,如果您在可以单击的页面的其他位置开始执行回滚,则会变得混乱。

The way I was thinking about fixing this issue is to have the form field binded to the controller properties and on each route copy the model properties to the controller properties on the setupController hook. 我考虑解决此问题的方法是将form字段绑定到控制器属性,并在每条路径上将模型属性复制到setupController钩子上的控制器属性。 This would prevent edits to impact the other routes. 这样可以防止编辑影响其他路线。

I am wondering if this best practice in ember or is there a better way to fix this issue? 我想知道这是灰烬最佳实践还是有解决此问题的更好方法?

Thank you 谢谢

You can use single rollback in deactivate route hook. 您可以在deactivate路由挂钩中使用单个回滚。 Then on cancel action you can do transition only. 然后,在cancel操作上,您只能进行过渡。

// edit challenge route
model(params) {
  ...
},

deactivate() {
  this.modelFor( this.get('routeName')).rollback();
}

PS Are you aware that rollback() still doesn't work properly with relations and reduced to rollbackAttributes() in ED 2.0? PS您是否知道rollback()仍不能与关系正常工作,并且在ED 2.0中简化为rollbackAttributes()

Related links: https://github.com/emberjs/data/issues/2122 https://github.com/emberjs/data/issues/3273#issuecomment-110965145 相关链接: https : //github.com/emberjs/data/issues/2122 https://github.com/emberjs/data/issues/3273#issuecomment-110965145

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

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