0
votes

To keep Ember focussed and to minimize data transfer, I "cached" some attributes on some models. Example: Because I only need the name of an author, I cache it to the book:

In Rails: Book has_one :author In Ember: Book author_name: DS.attr("string")

So in Rails I compute the author_name and send it with the json data.

Now if I try to send data back to the server, I need to omit the cached attributes.

I tried overriding "serialize" in the Book model but that had no effect (which struck me as odd and illogical).

Eventually I've overridden the addAttribute in my adapter. So I now use:

adapter: DS.RESTAdapter.extend
  serializer: DS.RESTSerializer.extend
    addAttribute: (hash, key, value)->
      cached_keys = [ "author_name", "more" ]
      this._super(hash, key, value) unless cached_keys.contains(key)

But now the attributes aren't linked to "Book" anymore. Even more, I have more cached attributes on other models, but in here they are all mixed together. I can't imagine this is the best way to solve the problem. Any better ideas?

1
I would tag this question with ember-data.mehulkar
@MehulKar I've added ember-data, thanksIvo Dancet

1 Answers

0
votes

I'm not on latest master, but I override addAttributes as opposed to addAttribute. Inside that I set up a switch statement to use record.constructor to serialize or not serialize values.

Example:

App.Serializer = DS.RESTSerializer.extend
  addAttributes: (data, record) ->
    switch record.constructor
      when App.Post
        record.eachAttribute (name, attribute) =>
          @_addAttribute(data, record, name, attribute.type) if name != "created_at"
      else
        @_super(data,record)