Ember js形式保留了旧的价值观

时间:2016-07-10 11:02:27

标签: ember.js ember-data ember-cli

我在这里尝试了其他答案,但没有一个帮助过。

app/routes/card/new.js

actions: {
  save(title, description) {
    const newCard = this.get('store').createRecord('card', { title, description } );
    newCard.save().then((card) => {
      this.transitionTo('card.card', card);
    });
  }
}

对于观点,我是:

app/templates/card/new.hbs

<form>
  <fieldset class="form-group">
    <label for="card-title">Card Title</label>
    {{input type="text" value=title class="form-control" id="card-title" placeholder="Enter the title of the card"}}
    <small class="text-muted">Give your card a nice title</small>
  </fieldset>

  <fieldset class="form-group">
    <label for="card-description">Card Description</label>
    {{textarea value=description class="form-control" id="card-description" rows="3"}}
    <small class="text-muted">Describe your card</small>
  </fieldset>

  <div class="btn-group" role="group" aria-label="Save or Cancel your card">
    <button {{action 'save' title description}} class="btn btn-secondary">Save</button>
    <button {{action 'cancel'}} class="btn btn-danger">Cancel</button>
  </div>
</form>

当我创建一张卡片时,它工作正常,但是当我再次尝试创建一辆新车时,该表单会保留旧值,这些值会在刷新时消失。

1 个答案:

答案 0 :(得分:1)

应用程序/路由/卡/ new.js

model(){
  return this.get('store').createRecord('card');
}

应用程序/控制器/卡/ new.js

actions: {
  save(){
   this.get('model').save().then((card) => {
      this.transitionTo('card.card', card);
    });
}
}

应用程序/模板/卡/ new.hbs

<form>
  <fieldset class="form-group">
    <label for="card-title">Card Title</label>
    {{input type="text" value=model.title class="form-control" id="card-title" placeholder="Enter the title of the card"}}
    <small class="text-muted">Give your card a nice title</small>
  </fieldset>

  <fieldset class="form-group">
    <label for="card-description">Card Description</label>
    {{textarea value=model.description class="form-control" id="card-description" rows="3"}}
    <small class="text-muted">Describe your card</small>
  </fieldset>

  <div class="btn-group" role="group" aria-label="Save or Cancel your card">
    <button {{action 'save'}} class="btn btn-secondary">Save</button>
    <button {{action 'cancel'}} class="btn btn-danger">Cancel</button>
  </div>
</form>