最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I save a model with attribute of type 'date' in Ember.js? - Stack Overflow

programmeradmin2浏览0评论

I'm trying to save an object containing a date attribute in Ember.js.

After calling createRecord() on the store, the new object is represented on the page, but the date attribute disappears from the page upon calling save(). All other attribute types on the model do not exhibit this appear-then-disappear behavior. Here is some example code:

App = Ember.Application.create()

App.IndexRoute = Ember.Route.extend
    model: () -> @store.find "post"

App.Post = DS.Model.extend
    time: DS.attr "date"
    body: DS.attr "string"

App.IndexController = Ember.ArrayController.extend
    actions:
        createPost: () ->
            body = @get 'newBody'
            post = @store.createRecord 'post',
                body: body
                time: Date.now()
            @set 'newBody', ''
            post.save()

App.Post.FIXTURES = [
    id: 1
    time: new Date("2013-9-30")
    body: "fixture post"
]

Please see this jsfiddle for a demonstration.

I'm trying to save an object containing a date attribute in Ember.js.

After calling createRecord() on the store, the new object is represented on the page, but the date attribute disappears from the page upon calling save(). All other attribute types on the model do not exhibit this appear-then-disappear behavior. Here is some example code:

App = Ember.Application.create()

App.IndexRoute = Ember.Route.extend
    model: () -> @store.find "post"

App.Post = DS.Model.extend
    time: DS.attr "date"
    body: DS.attr "string"

App.IndexController = Ember.ArrayController.extend
    actions:
        createPost: () ->
            body = @get 'newBody'
            post = @store.createRecord 'post',
                body: body
                time: Date.now()
            @set 'newBody', ''
            post.save()

App.Post.FIXTURES = [
    id: 1
    time: new Date("2013-9-30")
    body: "fixture post"
]

Please see this jsfiddle for a demonstration.

Share Improve this question edited Oct 12, 2013 at 1:14 Jordan asked Oct 12, 2013 at 0:18 JordanJordan 231 silver badge5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

Instead of using Date.now() you should use new Date().

Date.now() returns an integer which seems to give Ember problems. new Date() returns an actual Date object which Ember can deal with. Here's a slightly modified jsfiddle : http://jsfiddle/AJRts/

发布评论

评论列表(0)

  1. 暂无评论