New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for self-referential relationships between models when using AMD / RequireJS #127
Conversation
…rent model as the relatedModel. Added test to show this works.
Uhm.. self-referential relationships have always been supported. What's wrong with just using the name of the model itself? |
If you want use AMD with RequireJS for example, you can't use global scope to store models. So model should be something like:
But you can't use A because it's not define. |
Yes, thanks @Toobian, I should have explained. When loading relational models with Require.js they are not globally defined so can't be accessed via the text string model name method. |
I'll be using AMD in the near future as well, so this gets an 👍 from me! |
I don't think using some "magic" word syntax is the right approach here, as the problem appears to be broader than that (see #57). I'm not sure how scopes actually do work in that environment, but isn't there some (named) scope or scope object that could be added to |
I don't really understand what you mean. But I tried something that seemed good and it failed.
And after you can |
Well, I was trying to explain how we may be able to create something that does work (although I really don't know the specifics of the scope and security model used in node.js et al.). So it couldn't work yet anyway. What I'm thinking of is more like the following:
Where the implementation of
|
After look your proposition, I think it's good solution. I don't found better one. |
+1 for merging this. |
Okay, please give it a try with the current version. Really hope this does the trick. Example from the tests: var models = {};
Backbone.Relational.store.addModelScope( models );
models.Book = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasMany,
key: 'pages',
relatedModel: 'Page',
createModels: false,
reverseRelation: {
key: 'book'
}
}]
});
models.Page = Backbone.RelationalModel.extend();
var book = new models.Book();
var page = new models.Page({ book: book });
ok( book.relations.length === 1 );
ok( book.get( 'pages' ).length === 1 ); |
Works great. Thank you! |
It works but I'm running into trouble with naming conflicts now. I've got more models called "Page" in different scopes, someting that isn't uncommon with working with AMD modules. Also: how efficient will this be when you add heaps of scopes? In my experience, a better solution would be to let relationalModel accept a function that returns the model. It's actually something I've tried and works perfectly. It harnesses Javascript's expressive power and was actually suggested by @jrburke in #57. While using this, I've only ran into some problems with RelationalModel.fetchRelated(), but I'm not sure if that's related (am still a bit new to the code). |
+1 I totally agree. Current addScope method is not the best solutions and I ran into naming conflicts also. |
Also +1. Just came here to post aboit it and saw that I not the first one. |
+1 for having relationalModel accept a function as name conflicts between scopes exist. |
So using the example above I can get a self-referential relationship setup, however the |
wrt the following code, Backbone tries to fetch data for each model even if we add "autoFetch:false" explicitely.
|
If special keyword string "self" is used in relatedModel, use the current model as the relatedModel.
Added test to show this works.