Added full relations serialization in toJSON#183
Added full relations serialization in toJSON#183buzzeante wants to merge 9 commits intoPaulUithol:masterfrom
Conversation
|
I'm not opposed to this feature per se, but I don't see why you wouldn't just pass the model object itself to the template. As I understand and use it, |
|
I don't think there is a way of passing a model with all its relations expanded to a template. That's what I am trying to cover here. You are right about the way Cheers, |
|
What do you mean by "expanded"? Without the need to use the Another note: at the very least, a feature like this would need a big fat |
|
I mean that if I have a complex relational model with some relation defined with You are right @PaulUithol , there should be some warning to indicate a proper way of using it. In our case, we use it only occasionaly and never in loops avoiding performance isssues. Cheers, |
|
What templating system are you using? In most I am aware of, you can just pass the |
|
We are using the template system Mustache. Since it is logic-less, I believe we cannot access to javascript functions from the template. Hence, Cheers, |
There are some situations when we need to make toJSON work in
different ways:
- Communication with server where relations may be reduced to resource_uri
- Template rendering where we need more information that the one we send to the server
Adding `{full: true}` to toJSON allows to avoid `includeInJSON` params and make a full
serialization of a model and its relations.
|
All right, that makes sense. I'm not sure adding an option to |
|
That is another perspective we have considered but we decided to use Cheers, |
|
All right, "to be able to use it transparently with models that are not The imaginary Because of this, I think it's best to add the What do you think of this solution? The biggest (and really the only) issue I have with your implementation is the fact that it still respects Would you be up for making these changes? |
|
I see your point, but I don't know if an additional method I agree that respecting I guess this is more a matter of opinion, so I am open to make the changes in whichever direction you think fits best in the project. Cheers, |
|
It is definitely a matter of opinion, so here I am paging @PaulUithol to give his opinion, since this is his project first and foremost. Moreover I'll be gone from the internet for the next 6 days, so he'll probably be the one to merge this or some adaptation of this. Cheers! |
|
Sorry for the delay! I was caught up in side tasks. As I said before I am up to make the changes needed in the direction you guys believe is the way to go. So let me know how can I help. Cheers! |
|
I don't really have a preference here.. I've been trying to decide, but I can't ;). I'd like to avoid introducing additional methods if possible, but in this case it would be sort of inconsistent for |
|
Yeah, I am hesitant to introduce new methods if not absolute necessary, but it seems to me that this is a case where it would be appropriate. @buzzeante, are you up to make the changes? |
|
@buzzeante Are you still planning to look into this? |
|
Hello @DouweM . I am really sorry for this disappearance. These couple months have been really crazy. I plan on doing it this week. I will let you know as soon as I can tackle it :) Cheers, |
|
Perfect! |
|
I've been having a similar issue. That's why I added the pull request for adding the options param to the overridden toJSON function (just like the underlying Backbone toJSON has). #216 At least with that in place I can come up with my own solution until you guys work it out. FWIW, here's my current workaround: It lets me use the default configuration of the model relations as what will get set back to the server when toJSON is called without any options. But at anytime I can call It works OK for what I'm trying to do anyway. From what I understand reading this thread, you're mainly considering adding a 'full' option to return full nested model serialization. But like I show above, it would be very useful to have options for including or excluding whatever related models we like when calling toJSON. Also not keen on the proposed recursiveAttributes method. Backbone already has a toJSON method which people are familiar with and want to use whenever they need a JSON representation of a model. It just needs more options for working with backbone-relational and nested models :) Presumably, that's why there's an options param in the underlying Backbone.js library, even though it currently doesn't do anything. |
toJSON method will avoid relational configuration `includeInJSON` and `keyDestination` in case option `full=true` is included when calling the method.
|
Hello @DouweM, First of all, I am really sorry for this huge delay. At last I could take some time to continue with what I started. Taking into consideration all we talked about some time ago I just made some small changes in order to ignore I started doing a function I also included a test to check it works properly. Cheers! |
|
Hi @buzzeante, hi Javi, Well, I guess I'll have to apologize for a sizable delay as well! Relational has had to take a back seat for the last couple of weeks, unfortunately. I'm still very much in favor of Cheers, |
|
So just to be clear, the proposal is to have
|
|
Yup, |
|
So: |
|
Hey @DouweM, no problem, these dates are also pretty busy by default ;) Ok, I am on it and hope to have it soon enough. However I have to insist on the option In order to use Cheers, |
|
As mentioned 4 months ago:
With this, |
It encapsulates toJSON(full=true) functionality where keyDestination is omitted
|
Hey @DouweM I finally got the time to include Cheers, |
|
What's the deal with all of the changes in |
|
It's an identation fix. Sorry, didn't realise it was going to screw the diff in such way. Cheers |
|
Ah all right. Changes look good to me. Could you rebase it on top of master so I can easily merge it? |
There are some situations when we need to make toJSON work in
different ways:
- Communication with server where relations may be reduced to resource_uri
- Template rendering where we need more information that the one we send to the server
Adding `{full: true}` to toJSON allows to avoid `includeInJSON` params and make a full
serialization of a model and its relations.
toJSON method will avoid relational configuration `includeInJSON` and `keyDestination` in case option `full=true` is included when calling the method.
It encapsulates toJSON(full=true) functionality where keyDestination is omitted
…lational into feature/full-toJSON Conflicts: backbone-relational.js
|
There you are! It's the first time I do a rebase so it may be a bit of a mess I'm afraid. |
|
Haha. Something definitely went wrong, there's 10 commits in the pull request now. No problem though, I'll clean it up manually before merging. |
|
Thanks @DouweM! |
|
Hello @dowem, I just realized there is a huge bug in the Pull Request when there is a HasMany relationship since Backbone.Collection does not have an implementation of I can see 2 solutions for it: Go back to previous implementation or extend What do you think? Cheers, |
|
Hi @buzzeante, Ah damn, you're right. I think there's a third solution though: add a simple if/else statement to check if I'm sorry by the way for the month of no response, I'm so busy these days it probably isn't healthy. Cheers, |
|
No worries @DouweM :) I already solved it in my branch with your approach. I will write some test so that it checks it runs and push it. Cheers, |
|
Hi yet again. Added the tests, and the bug fix for the issue we mentioned. I am really sorry for such delays, I am stuffed up with work too :S |
|
Looks good. Now it's up to me to consolidate the commits. |
|
Please rebase against master, because you have worked on 0.7 version |
|
Ping @buzzeante, nice work, I'd like to see it merged. |
|
This is a very useful change for those of us who are used to using toJSON for serialization of models for use in rendering templates. Is there any particular reason why this wasn't merged? |
|
Thanks a lot for this solution. Needed it a model to its previous to previous state with nested collections. @mtsr Up. Would it be possible to fix the conflicts and merge it? |
|
I allowed myself to duplicate the merge request. So there is no longer merge conflicts. |
|
Closing this in favor of the cleaned up PR #534 |
Hello there,
There are some situations when we need to make
toJSONwork indifferent ways:
resource_uriAdding
{full: true}to toJSON allows to avoidincludeInJSONparams and make a fullserialization of a model and its relations.
Related: GH-161.
Cheers,
Javi