Here is the code for a model I've written:
export default Ember.Route.extend({
orgs: [],
superOrgRecord: null,
getsuperOrg: Ember.computed('orgs', function () {
var super_org = this.get('orgs').findBy('name', 'test org');
var super_org_record = this.store.peekRecord('org', super_org.id);
for (var i = 0; i < super_org.users.length; i++) {
var super_org_user = this.store.createRecord('user', { name: super_org.users[i].name,
org: super_org_record });
for (var j = 0; j < super_org.users[i].roles.length; j++) {
super_org_user.get('roles').addObject(this.store.createRecord('role', { name: super_org.users[i].roles[j].name }));
}
}
this.setProperties({superOrgRecord: super_org_record});
return super_org_record;
}),
redirect: function() {
var user = this.modelFor('application').user;
if(Ember.isEmpty(user.get('auth')) || Ember.isEmpty(user.get('org').get("id"))){
this.transitionTo('login');
}
},
beforeModel: function() {
var _this = this;
return ajax({
url: _this.modelFor('application').url + '/org_user_list.json/?auth_token=' + _this.modelFor('application').user.get('auth'),
type: 'get',
}).then(
function(result) {
_this.setProperties({
orgs: result,
});
},
function(error) {
alert(error.jqXHR.responseText);
}
);
},
model: function() {
return Ember.RSVP.hash({
orgs: this.get('orgs'),
superOrg: this.get('getsuperOrg')
});
},
// TODO I thought this would fix duplicating the super org records but it didn't
// deactivate: function() {
// if (this.superOrgRecord) {
// this.superOrgRecord.get('users').forEach( function(user) {
// user.get('roles').forEach( function(role) {
// this.store.unloadRecord(role); // also tried deleteRecord()
// });
// this.store.unloadRecord(user);
// });
// }
// },
actions: {
error(error) {
if (error.message === "Cannot read property 'id' of undefined") {
this.redirect();
} else {
return;
}
}
// TODO I thought this would fix duplicating the super org records but it didn't
// willTransition(transition) {
// if (this.superOrgRecord) {
// this.superOrgRecord.get('users').forEach( function(user) {
// var roleObjects = user.get('roles');
// user.get('roles').removeObjects(roleObjects);
// });
// var userObjects = this.superOrgRecord.get('users');
// this.superOrgRecord.get('users').removeObjects(userObjects);
// }
// return true;
// }
}
});
The computed property getsuperOrg does its job correctly. It runs every time this route is loaded (in the model method). Now, when I transition away from this model, I want to remove all references to these user and role objects that I have just added to the store, as they were just temporary for the purposes of returning the values to be generated by the model call (you can question my reasons for doing this, let's just assume for now it has to be done this way). When I reload this model, there ends up being duplicates of these items in the store, and messes up the rendered results of my components later on. I've also noted in the code (commented out) a couple different techniques have tried to accomplish this, with both deactivate and willTransition. Both execute without error, but neither actually does the job -- when I load this model again for a second or subsequent time, additional users and roles continue to be rendered.
Please help; thanks.
Aucun commentaire:
Enregistrer un commentaire