to to another resource throwing uncaught #error
I have a model which 'owns' other models (belongTo, hasMany) and I'm attempting to create each in a multi-step form.
I am passing the parent model, campaign
, through with each link-to
so it can be used when creating the other records and displaying existing children. This worked when I originally had a nested route
, but when I change it to a resource
the campaign
model seemingly isn't sent through.
My router:
App.Router.map(function () {
this.resource('campaigns', function() {
// I originally had a this.route('step2') which I sent the model to and it works
this.resource('campaign', { path: '/:campaign_id' }, function() {
this.route('edit');
this.resource('ad-groups', function() {
this.route('new'); // new route is irrelevant in this question
});
});
});
});
So in the campaign/edit
router, I save the model and transition to the ad-groups/index
route with the campaign as the model:
App.CampaignEditRoute = Ember.Route.extend({
actions: {
save: function(campaign) {
campaign.save().then(function() {
this.transitionTo('ad-groups', campaign);
}.bind(this));
}
}
});
But this is where it renders the template, but the Ember inspector shows it's not loading a model. And the relevant parts of my models for completeness:
App.Campaign = DS.Model.extend({
adGroups: DS.hasMany('ad-group', { async:true }),
...
});
App.AdGroup = DS.Model.extend({
campaign: DS.belongsTo('campaign'),
...
});
If I manually return the campaign within the model
hook of ad-groups/index
then Ember throws an Uncaught #error
, and renders the template but without any of the model data showing.
Edit: So I tried implementing the solution below by setting the model
as this.modelFor('campaign')
and it threw another Uncaught #error
. Bizarrely, when I remove the relationship in my FIXTURES it no longer errors, but of course I lose that relationship. My fixtures:
App.Campaign.FIXTURES = [
// If I remove the adGroups: [1] it removes the Uncaught #error but I lose the relationship
{ id: 1, name: "Campaign #1", app: 1, adGroups: [1] },
{ id: 2, name: "Campaign #2", app: 1 }
];
App.AdGroup.FIXTURES = [
{ id: 1, name: 'My first AdGroup', bid: 2, budget: 100, campaign: 1 }
];
I had a similar issue to this before, and I fixed it by adding { async: true }
to my campaign
model as you can see above. So this looks like it's more the issue, admittedly async confuses me a little still. Could anyone shed some light on why this might be happening?
After all of this, the two part solution was:
1.) Instead of passing the model
using the link-to
, implementing this:
App.MyRoute = Ember.Route.extend({
model: function() {
return this.modelFor('campaign');
},
...
});
Which in my opinion shouldn't be necessary, but it's actually really nice in that it uses a parent routes' model - link for reference.
2.) The remaining Uncaught #error
error that Ember was throwing was extremely unhelpful. It turns out I was linking to adgroup
instead of ad-group
in the template:
{{link-to 'adgroup'}}
instead of {{link-to 'ad-group'}}
上一篇: 灰烬分选有很多关系
下一篇: 到另一个资源抛出未捕获的#error