如何将查询参数的值传递到表单中



我的URL看起来像http://localhost:4099/checkout/schedule/new?addressId=12我正在尝试将查询参数addressId传递给表单。

我尝试将其作为隐藏输入提交,但当它命中save操作时。我检查了 Ember 检查器的Network选项卡,这就是它传递的内容:

{"delivery":{"instructions":"foo","deliver_on":"bar","address_id":null}}

address_id仍然null.我错过了什么?

完整代码如下:

// app/pods/checkout/schedule/new/route.js
import Ember from 'ember';
export default Ember.Route.extend({
  model: function() {
    return this.store.createRecord('delivery');
    // return this.store.createRecord('delivery', { addressId: this.get('addressId')});
  },
  // Cleanup the controller, when you leave the new route so the stale new record is also
  // removed from the store.
  // You can also use https://github.com/dockyard/ember-data-route instead
  resetController: function (controller, isExiting) {
    var model = controller.get('model');
    if (!model.get('isDeleted') && isExiting && model.get('isNew')) {
      model.deleteRecord();
    } else {
      model.rollback();
    }
  }
});
// app/pods/checkout/schedule/new/controller.js
import Ember from 'ember';
export default Ember.Controller.extend({
  queryParams: ['addressId'],
  addressId: null,
  actions: {
    save: function() {
      var _this = this;
      // this.get('model').set('addressId', this.get('addressId'));
      this.get('model').save().then(function(){
        _this.transitionToRoute('checkout.address.index');
      }, function() {
        // Need this promise, so we can render errors, if any, in the form
      });
      return false;
    },
    cancel: function() {
      return true;
    }
  }
});
// app/pods/checkout/schedule/new/template.hbs
<form {{action "save" on="submit"}}>
  {{addressId}}
  {{input type="hidden" value=addressId}}
  <p>
    <label>Instructions:
      {{input value=model.instructions}}
    </label>
    {{#each error in errors.instructions}}
      <br />{{error.message}}
    {{/each}}
  </p>
  <p>
    <label>Deliver on:
      {{input value=model.DeliverOn}}
    </label>
    {{#each error in errors.DeliverOn}}
      <br />{{error.message}}
    {{/each}}
  </p>
  <input type="submit" value="Next"/>
  <button {{action "cancel"}}>Cancel</button>
</form>
// app/models/delivery.js
import DS from 'ember-data';
export default DS.Model.extend({
  address:      DS.belongsTo('address', { async: true }),
  items:        DS.hasMany('item', { async: true }),
  instructions: DS.attr('string'),
  deliverOn:    DS.attr('string')
});
我相信

正在发生的事情是你并没有真正提交你的表格。相反,您正在对提交模型数据的模型调用save()。因此,表单中的隐藏参数在这里对您没有帮助。

您在网址中的addressId控制器中的addressId资源相关联,而您在 Chrome 中看到的addressId: null模型中addressId媒体资源的值

相关内容

  • 没有找到相关文章

最新更新