Closed mrusme closed 8 years ago
Thank you for your efforts, but I don't think it should be inside Mongorito, because toJSON()
is kind of a standard way to provide serialized data. Check out toJSON()
in Backbone for a reference.
@vdemedes thank you for your reply! Can you think of any better place to integrate such a feature? The use case I'm trying to solve is: My Model contains an attribute named password
, which (of course) doesn't need to be exported to JSON (-> returned via API). Therefor I tried to build a generic way to allow the exclusion of any attribute from being exported to JSON. The idea for this was basically taken from Laravel's Eloquent.
I'd suggest adding a new method specifically for this use case, like toFilteredJSON
or something.
@mrusme Why not just leave it as an option in queries or perhaps in the collection class; attributes: false
?
@vdemedes what would be the actual benefit? I only see the downside of being unable to work with models as before, requiring to fulfill(model.toFilteredJSON())
instead of simply fulfill(model)
. Besides, I'm not sure whether a separation would actually make sense here, as toJSON would only filter, if hidden-attributes were defined.
@ulpian it's not query related, that's why. Maybe I didn't understand your suggestion correctly, but we're talking about the model toJSON conversion here. It wouldn't make sense to set this inside the actual query, as the model internal data wouldn't be affected of this. This means: One definitely wants to have the password hash inside the model, so one can work with it (e.g. hash it using the entered password to see, whether it's correct) - one might just not want to have to return the data like this:
let data = clone(model.get());
delete data.password;
fulfill(data);
As stated before, I think the more elegant solution here would be having it handled automatically be the generic toJSON
method for the model, if hidden-attributes where configured inside the model's configure()
method.
I've created a new PR in order to be able to continue the work with my actual project: https://github.com/vdemedes/mongorito/pull/120 - sorry for inconveniences!
I was looking for a way to hide specific attributes inside the toJSON() transformation of a model and quickly implemented something here. Let me know what you think of it. If you're okay with the implementation, I can extend the PR with documentation + tests. Regards!