0

My URL looks like http://localhost:4099/checkout/schedule/new?addressId=12 I am trying to pass the query param addressId to the form.

I've tried submitting it as a hidden input, but by the time it hits the save action. I check the Network tab of Ember inspector and this is what it is passing:

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

address_id is still null. What am I missing?

Full code below:

// 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')
});
4

1 回答 1

0

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

addressId在 URL 中与您addressId控制器中的属性相关联,您addressId: null在 Chrome 中看到的提交是模型addressId中属性的值

于 2015-01-19T15:43:02.583 回答