Skip to content
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

reverseRelation does not work? #493

Open
USvER opened this issue Oct 1, 2014 · 4 comments
Open

reverseRelation does not work? #493

USvER opened this issue Oct 1, 2014 · 4 comments
Labels

Comments

@USvER
Copy link

USvER commented Oct 1, 2014

Week HasMany Days with reverseRelation
I want to get Week from any Day, but day.get('week') returns null

So the code is:

var Day = Backbone.RelationalModel.extend({
  initialize: function(){
    alert(this.get('week')); // Alerts null but should be 'first' week
  }
});
var Week = Backbone.RelationalModel.extend({
  relations: [{
    type: Backbone.HasMany,
    key: 'days',
    relatedModel: Day,
    reverseRelation: {
      key: 'week',
    }
  }]
});
first = new Week;
first.get('days').create({name: "Monday"});

If reverseRelation is not meant to work like that, how can i get this functionality?
PS: Here is the code http://jsfiddle.net/77rmbevw/1/

@tjwebb
Copy link

tjwebb commented Nov 18, 2014

#503

@linus-amg
Copy link

i would also really like to understand that, i have the same "issue"

@4d4178
Copy link

4d4178 commented Dec 8, 2014

That's because when initialize function is called, the model does not have relations yet. you could listen to change:week and call your function:

var Day = Backbone.RelationalModel.extend({
    initialize: function() {
    this.listenTo(this, 'change:week', function() {
        alert(this.get('week'));
    });
    }
});

http://jsfiddle.net/77rmbevw/3/

@RalphSleigh
Copy link

I have a similar issue:

  1. Marionette composite view listening to an initially empty hasMany collection
  2. Call fetch on the collection to load some related models from server
  3. The models come, get added to the collection
  4. add triggered on collection, causing the view to be rendered.
  5. Inverse relation is then set on the new models

My problem is that my view depends on the inverse relation to pull a field from the parent model, so, #5 needs to happen before #4.

Current workaround plan is when I call fetch, stash the current parent model in some global state then get it out in an event attached to my new models being added to the collection.

TLDR: Everything should be in place before views render anything, currently not.

@bpatram bpatram added the bug label Mar 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants