LearnBoost/mongoose — pull request #991 by STRML (2012-07-02 15:27:13+00:00)
Title: Added suffix support to populate() for frameworks like ExtJS

ExtJS doesn't play well with the populate() function in Mongoose. ExtJS expects a certain field to be populated with an association, or null if the object is not denormalized. The problem is that if I choose not to denormalize an object and send it down to ExtJS, its Model structure will attempt to treat the object ID as an actual Model and fail.

Call populate(path, fields, conditions, options) with options.suffix defined, and the populated object will go into a new field, key+suffix, instead of overwriting the key field.

For example, for compatibility with ExtJS4, we might want to send down an object in this manner to be friendly to ExtJS' association support:

User.findById("id").populate('comment', null, null, {suffix: '_obj'}).run(function(err, user){...});

Which yields:

User{
...
comment: "4f5cd5f6e9786ec113000006"
comment_obj: {text: "foo"}
...
}

ExtJS is then instructed that the associationKey is 'comment_obj'. This means it will correctly use the contents of 'comment_obj' to construct a model when it is present, but will not if it is not present.