简体   繁体   中英

How to serialise ember date?

I'm new to ember and I want to post a new record, So I did something like:

App.AdminController = Em.Controller.extend
  submit: ->
    post = App.Post.createRecord()
    post.set('author', $('#author').val())
    post.set('title', $('#title').val())
    post.set('intro', $('#intro').val())
    post.set('description', $('#description').val())
    post.set('publishedAt', new Date())
    post.get('store').commit()

Everything works like a charm, except the publisedAt attribute e in post request json file is null

在此处输入图片说明

I think the problem is due to that I may not serialise it correctly, any idea?


update the model:

App.Post = DS.Model.extend
  title: DS.attr('string')
  author: DS.attr('string')
  intro: DS.attr('string')
  description: DS.attr('string')
  publishedat:DS.attr('date')

尝试在Date对象上使用JSON.stringify:

post.set('publishedAt', JSON.stringify(new Date()))

For some reason, you can't invoke new Date() inside the .set method. This should work:

    var d = new Date();
    post.set('publishedAt', d);

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