admin管理员组

文章数量:1356808

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/

本文标签: javascriptHow do I save a model with attribute of type 39date39 in EmberjsStack Overflow