Having a bit of trouble getting the hasOne relation working within Angular.
I'm building a survey app, with questions that can be reusable amongst surveys. I have two question models to represent this: SurveyQuestion and Question.
SurveyQuestion has a hasOne relation to Question
Question has two properties: text (the question text) and type (checkbox, radio button, text input, etc.)
Here's the JSON (removed parts that aren't applicable):
// SurveyQuestion Model
{
"name": "SurveyQuestion",
"plural": "surveyquestions",
"base": "PersistedModel",
"idInjection": true,
"properties": {},
"validations": [],
"relations": {
"question": {
"type": "hasOne",
"model": "Question",
"foreignKey": "questionId"
}
},
"acls": [],
"methods": []
}
// Question Model
{
"name": "Question",
"plural": "questions",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"text": {
"type": "string",
"required": true
},
"type": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}
Here's my current solution for setting this up in Angular:
Survey.questions.create({ id:
vm.survey.id }, {}).$promise.then(function(surveyQuestion) {
console.log('survey question created!', surveyQuestion);
SurveyQuestion.question.create({ id: surveyQuestion.id }, {
text: 'What is your name?',
type: 'text input'
}).$promise.then(function(question) {
console.log('question created!', question);
});
});
The above does in fact create a SurveyQuestion instance and a Question instance, but both come back with a 'questionId' property which is actually the ID of the SurveyQuestion and not the Question.
Hope that wasn't too confusing, any ideas? Or better ideas for handling the Angular side?