About the parse method on models.

I've been spending some time recently implementing my own take on relational models while trying to play nice with the current enyo MVC components as much as possible and I've ran into an interesting 'oddity'.

enyo.Model's parse method gets executed on at least two occations: When fetching a record, and when instantiating a new record. To me this seems a little bit odd that these very different use-cases always use the same parsing method, rather than making it's use optional.

For an example of why I think this is odd, consider the following:

I'm working with an api that's returns not just one or more records, but also a status code, possible error messages, etc. So I implemented a parse method to return just the record's attributes and discard other meta-data returned by the api.

However, there are times when I'd like to simply instantiate a new record, and pass it an object. This object doesn't need to be parsed and as such, it's rather pointless to pass it on to the parsing method.

This oddness isn't limited to my personal use-case though. There is even at least one use-case where enyo itself seems to struggle with this setup as well:

If I'm using the merge strategy when fetching an enyo.collection, it'll treat the incoming data as a plain old javascript object containing the records' attributes, or an instantiated enyo.Model. Then, regardless of asserting a data format that requires no parsing, it goes on moments later to instantiate newly added records *with* a non-optional call to the parse method in the constructor.

In the spirit of not re-inventing the wheel too much and borrowing some of Backbone's good practices, I would like to propose making parsing at construction time, and parsing after fetching data optional.

I was wondering if any other enyo devs here have some thoughts about the pro's and cons of this proposal before I go ahead and an open an issue for this.
Sign In or Register to comment.