1

Ember 默认观察到我的所有模型属性,因此当我在表单中创建新记录时,我的应用程序正在更新所有使用模型的位置(帖子计数器、带有标题的标题等),

在我的记录有效并保存在数据库中之前,我不想要这样的行为。

控制器:

ProManager.TasksNewController = Ember.ObjectController.extend
  headerTitle: 'Create'
  buttonTitle: 'Create'

  actions:
   save: ->
     if @content.validate()
     @content.save().then (=>
       @transitionToRoute('task', @content)
     ), (error) =>
       @content.becameError()
    else
      @content.set('errors', @content.get('validationErrors.fullMessages'))

模板:

<form id="task-form">
<fieldset>
    <div>
        <label {{bindAttr for="titleField.elementId"}}>Title</label>
        {{view Ember.TextField valueBinding='title' name='title' viewName='titleField'}}
    </div>
    <div>
        <label {{bindAttr for='descriptionField.elementId'}}>Last Name</label>
        {{view Ember.TextField valueBinding='description' name='description' viewName='descriptionField'}}
    </div>
    <a href='#' {{action save}} class='btn btn-success'>{{buttonTitle}}</a>
</fieldset>

在保存之前,我不想在页面上更新/显示 {{title}} 或 {{description}}:

感谢帮助 :)

这是一个很好的例子: http: //jsbin.com/arebem/2/edit,当创建新用户时,first_name 在保存之前在用户列表中更新。

好的,我对此有某种解决方案,我们只能在它不脏的时候展示它。

{{#unless isDirty}}
<div class="accordion-on">
    <h3>#{{id}} {{title}}</h3>
    <div>
        <div>{{title}}</div>
        <div>{{description}}</div>
    </div>
</div>
{{/unless}}
4

1 回答 1

0

解决方案模板#1

{{#unless isDirty}}
<div id="task-lists" class="well">
    <h5>#{{id}} {{title}}</h5>
    <button class="btn btn-small" {{action 'openModal' 'TasksNew' this 'Task' }}>New Task</button>

    {{#each task in content.tasks}}
        {{ render "tasks/show_accordion" task }}
    {{/each}}
</div>
{{/unless}}

解决方案模板 #2(如果未保存,则设置隐藏的 css 类)

<div {{bind-attr class=":accordion isDirty:hidden"}}>

<h3>#{{id}} {{title}}</h3>
<div>
    <div>{{title}}</div>
    <div>{{description}}</div>
</div>
</div>
于 2013-11-05T21:07:29.590 回答