Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Model attributes that are Model/Collection serialization #829

Open
KATT opened this issue Aug 22, 2014 · 0 comments
Open

Model attributes that are Model/Collection serialization #829

KATT opened this issue Aug 22, 2014 · 0 comments

Comments

@KATT
Copy link

KATT commented Aug 22, 2014

If I have an model A, which contains object b, then serialize object a, I would it expect b.serialize() to be called as a part of a.serialize(). This is currently not the case.

I'm making a CompositeModel which essentially works as a group of models (for convenience and is itself not synced) and want it's models to decide of they serialize themselves. Here is my workaround there:

utils = require 'lib/utils'

Model = require 'models/base/model'

serializeAttributes = (model, attributes, modelStack) ->
  # Create a delegator object.
  delegator = utils.beget attributes

  # Map model/collection to their attributes. Create a property
  # on the delegator that shadows the original attribute.
  for key, value of attributes
    delegator[key] = utils.serialize(value)


  # Return the delegator.
  return delegator

module.exports = class CompositeModel extends Model
  serialize: ->
    serializeAttributes this, @getAttributes()

  fetch: ->
    promises = []
    attributes = @getAttributes()

    for key, value of attributes
      if typeof value.fetch is 'function'
        promises.push value.fetch()


    return $.when.apply($, promises)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant