Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date and DateTime Transforms (WIP) #61

Closed
toranb opened this issue Nov 13, 2013 · 13 comments
Closed

Date and DateTime Transforms (WIP) #61

toranb opened this issue Nov 13, 2013 · 13 comments

Comments

@toranb
Copy link
Owner

toranb commented Nov 13, 2013

Currently you need to manually wire up date and datetime to work with DRF (the below is a workaround that works with the current build). I plan to include this in a future release (need to get some testing around this before adding it directly to the adapter)

Ignore the duplication for the moment

DS.IsodatetimeTransform = DS.Transform.extend({
    deserialize: function(serialized) {
        var type = typeof serialized;
        if (type === "string") {
            return new Date(Ember.Date.parse(serialized));
        } else if (type === "number") {
            return new Date(serialized);
        } else if (serialized === null || serialized === undefined) {
            // if the value is not present in the data,
            // return undefined, not null.
            return serialized;
        } else {
            return null;
        }
    },
    serialize: function(date) {
        if (date instanceof Date && date.toString() !== 'Invalid Date') {
            return date.toJSON();
        } else {
            return null;
        }
    }
});

DS.IsodateTransform = DS.Transform.extend({
    deserialize: function(serialized) {
        var type = typeof serialized;
        if (type === "string") {
            return new Date(Ember.Date.parse(serialized));
        } else if (type === "number") {
            return new Date(serialized);
        } else if (serialized === null || serialized === undefined) {
            // if the value is not present in the data,
            // return undefined, not null.
            return serialized;
        } else {
            return null;
        }
    },
    serialize: function(date) {
        if (date instanceof Date && date.toString() !== 'Invalid Date') {
            return date.toISOString().slice(0, 10);
        } else {
            return null;
        }
    }
});

Ember.Application.initializer({
    name: "DjangoRESTAdaptertransforms",
    after: "transforms",
    initialize: function(container, application) {
        application.register('transform:isodate', DS.IsodateTransform);
        application.register('transform:isodatetime', DS.IsodatetimeTransform);
    }
});
@hnqso
Copy link

hnqso commented Apr 1, 2014

Hi @toranb, is it related with the error Datetime provided to field must be a string?

@toranb
Copy link
Owner Author

toranb commented Apr 1, 2014

What do your models look like when you get this error?

@hnqso
Copy link

hnqso commented Apr 1, 2014

@toranb it looks alright here. I'm trying to save the follow model but I'm having issues with pub_date field generating that error.

class Board(models.Model):
  title = models.CharField(max_length=200, blank=True)
  body = models.TextField(blank=True)
  image = models.ImageField(upload_to='upload/images/boards', max_length=200, blank=True)
  pub_date = models.DateTimeField('date published')

I've created a gist with the full model and JavaScript https://gist.github.com/henriquea/9914529. Am I missing something?

@toranb
Copy link
Owner Author

toranb commented Apr 2, 2014

Excellent - you are correct. In your ember model for this do the following

App.Board = DS.Model.extend({
    pubDate: attr('isodatetime')
});

Then you need to setup / register the isodatetime transform so it works with ember and django

DS.IsodatetimeTransform = DS.Transform.extend({
    deserialize: function(serialized) {
        var type = typeof serialized;
        if (type === "string") {
            return new Date(Ember.Date.parse(serialized));
        } else if (type === "number") {
            return new Date(serialized);
        } else if (serialized === null || serialized === undefined) {
            // if the value is not present in the data,
            // return undefined, not null.
            return serialized;
        } else {
            return null;
        }
    },
    serialize: function(date) {
        if (date instanceof Date && date.toString() !== 'Invalid Date') {
            return date.toJSON();
        } else {
            return null;
        }
    }
});

Ember.Application.initializer({
    name: "DjangoRESTAdaptertransforms",
    after: "transforms",
    initialize: function(container, application) {
        application.register('transform:isodatetime', DS.IsodatetimeTransform);
    }
});

Let me know if this gets you moving. This is what I'm doing in my commercial app today

@hnqso
Copy link

hnqso commented Apr 5, 2014

I did this but was missing the 'attr' isodate. Thanks @toranb

@valberg
Copy link

valberg commented Jul 2, 2014

Any news on when this gets merged into a release?

@toranb
Copy link
Owner Author

toranb commented Jul 4, 2014

@valberg honestly if you wanted to submit a PR that has this included (and throw a few tests into show how it works when a xhr is fired off) I'd pull it in. For better or worse I don't work with ember day-to-day anymore so I'm not sure if the community needs or wants this /how it might impact other using the library in production today

@dbinetti
Copy link

I am not qualified to write a PR, but FWIW I do need this functionality and the workaround does the job for me.

@dustinfarris
Copy link
Collaborator

Thanks for the feedback @dbinetti. We will get this into the next release.

@dbinetti
Copy link

great!  

and while I’m asking for free stuff what about pagination?  :-P

On Wed, Jul 16, 2014 at 12:41 PM, Dustin Farris [email protected]
wrote:

Thanks for the feedback @dbinetti. We will get this into the next release.

Reply to this email directly or view it on GitHub:
#61 (comment)

@dustinfarris
Copy link
Collaborator

@dbinetti there is an ongoing conversation in #80. tl;dr we are waiting on Ember 1.7 stable which should support queryParams, making pagination a lot more straight-forward.

@dbinetti
Copy link

yep, makes sense.  looking forward to that as well!  

many thanks.

On Wed, Jul 16, 2014 at 12:48 PM, Dustin Farris [email protected]
wrote:

@dbinetti there is an ongoing conversation in #80. tl;dr we are waiting on Ember 1.7 stable which should support queryParams, making pagination a lot more straight-forward.

Reply to this email directly or view it on GitHub:
#61 (comment)

@dustinfarris
Copy link
Collaborator

Closing in favor of #96.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants