Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Re : Model Implied Relationships & Serialization = Too Much Recursion?

$
0
0
Here's where I'm at.  I've set up a base model to inherit from with a static property called "serialization_keys" that is a list of attributes to include in serialization.  Then I created my own serialization method:

  1.        serialize: function() {
  2.             var self = this;
  3.             var serialized = {};

  4.             var attribute_keys           = this.constructor.keys(this);
  5.             var serialization_white_list = this.constructor.serialization_keys || attribute_keys;

  6.             $.each(attribute_keys, function(i, attr_key){
  7.                 if (serialization_white_list.indexOf(attr_key) === -1) return true;
  8.                 serialized[attr_key] = self.attr(attr_key);
  9.             });

  10.             return this._super.apply(new this.constructor(serialized), arguments);
  11.         }

Basically if serialization_keys is not defined it serializes everything.  if it is defined, it skips anything not in the list.  It creates a new temporary model instance and serializes it with only the necessary keys.  

This allows me to specify that the user should be skipped when serializing an activity.

Not ideal but it seems to do the trick.  Anyone have a better solution?


Viewing all articles
Browse latest Browse all 3491

Trending Articles