Skip to content

Instantly share code, notes, and snippets.

@thebigredgeek
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thebigredgeek/f2eec368e59a2dbfb27b to your computer and use it in GitHub Desktop.
Save thebigredgeek/f2eec368e59a2dbfb27b to your computer and use it in GitHub Desktop.
DS#loadRelations not working
'use strict';
angular.module('console').factory('Employer', function(DS, $rootScope){
var store = DS.defineResource({
name: 'Employer',
idAttribute: 'guid',
endpoint: '/employers',
keepChangeHistory: true,
schema: {
name: 'string',
total_employees: 'number'
},
hasMany: {
'EmployerDirector': {
localField: 'employerDirectors',
foreignKey: 'employer_guid'
}
}
});
$rootScope.$on('$unauthorized', function(){
store.ejectAll();
});
return store;
}).run(['Employer', angular.noop]);
angular.module('console').factory('EmployerDirector', function(DS, $rootScope){
var store = DS.defineResource({
name: 'EmployerDirector',
idAttribute: 'guid',
endpoint: '/employer_directors',
keepChangeHistory: true,
schema: {
employer_guid: 'string',
director_guid: 'string',
},
relations: {
hasOne: {
'Director': {
localField: 'director',
localKey: 'director_guid'
}
},
belongsTo: {
'Employer': {
localField: 'employer',
localKey: 'employer_guid',
parent: true
}
}
}
});
$rootScope.$on('$unauthorized', function(){
store.ejectAll();
});
return store;
}).run(['EmployerDirector', angular.noop]);
angular.module('console').factory('ResolveEmployers', function(Employer, EmployerDirector, $q, $rootScope){
return function () {
return (
Employer.getAll().length ?
$q.when(Employer.getAll()) :
Employer.findAll()
)
.then(function(employers){
if (employers.length) {
return $q.all((employers || []).map(function(employer){
console.log(employer.guid);
return $q.when(Employer.loadRelations(employer.guid));
}));
} else {
return $q.reject();
}
});
};
});
angular.module('console').run(function(ResolveEmployers){ResolveEmployers();});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment