简体   繁体   中英

Ember set date field that is using DS.attr(“date”)

I have a simple model Expense which has date, amount, description .

My problem is, when the record is sent to the server, the date field is always nil , only works if I leave it in the default value.

The model has a defualt value for date. When the form is rendered, the default date is prepopulated. If this form is submitted, the value is sent. If I do click on the input field(focus), it would always submit nil to the server.

Am I doing something wrong?

Code below. Sorry if it looks alien, I'm using coffeescript =)

Received params

{"expense"=>{"amount"=>10, "date"=>nil, "description"=>"Hello I got an expense"}}

Model

Household.Expense = DS.Model.extend
  amount:      DS.attr('number')
  date:        DS.attr('date', { defaultValue: new Date})
...

View

Household.ExpensesNewView = Ember.View.extend()

Controller

Household.ExpensesNewController = Ember.ObjectController.extend
  create: ->
    @transaction.commit()

  init: ->
    @transaction = @.get('store').transaction()
    @set 'content', @transaction.createRecord(Household.Expense)

  save: ->
    @transaction.commit()
    this.transaction = null

  transitionAfterSave: (->
    if @get('content.id')
      @transitionToRoute("expenses")).observes('content.id')

When an input field like the below is used:

<form {{action save on="submit"}} class="form-horizontal">
  {{view Ember.TextField valueBinding="date" id="date" placeholder="Date" required="true"}}
</form>
@transaction.set('date', new Date(this.get('date')));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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