toranb / ember-data-django-rest-adapter

An ember-data adapter for django web applications powered by the django-rest-framework
MIT License
152 stars 26 forks source link

Date and DateTime Transforms (WIP) #61

Closed toranb closed 10 years ago

toranb commented 11 years ago

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 commented 10 years ago

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

toranb commented 10 years ago

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

hnqso commented 10 years ago

@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 commented 10 years ago

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 commented 10 years ago

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

valberg commented 10 years ago

Any news on when this gets merged into a release?

toranb commented 10 years ago

@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 commented 10 years ago

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

dustinfarris commented 10 years ago

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

dbinetti commented 10 years ago

great!  

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

On Wed, Jul 16, 2014 at 12:41 PM, Dustin Farris notifications@github.com wrote:

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

Reply to this email directly or view it on GitHub: https://github.com/toranb/ember-data-django-rest-adapter/issues/61#issuecomment-49216210

dustinfarris commented 10 years ago

@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 commented 10 years ago

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

many thanks.

On Wed, Jul 16, 2014 at 12:48 PM, Dustin Farris notifications@github.com 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: https://github.com/toranb/ember-data-django-rest-adapter/issues/61#issuecomment-49217007

dustinfarris commented 10 years ago

Closing in favor of #96.