Domain model
var Question = function(text, answerType){
this.id = 0
this.text = text;
}
var Form = function(){
this.questions = []
this.addQuestion = function(question){
question.id = this.questions.length + 1
this.questions.push(question)
}
}
Renders a Form
var FormRenderer = function(){
this.wrapperSelector = "#wrapper"
this.render = function(form){
// how to access renderQuestion member of FormRenderer within the higher-order function?
$(form.questions).each( function(){ **this.renderQuestion(form, this) }**)
}
this.renderQuestion = function(form, question){
var questionDomId = "question" + question.id
var questionText = '<input type="text" size="75" name="questionText" value="'+ question.text +'" /><br/><br/><br/>'
var questionWrapper = "<div id='" + questionDomId + "'>" + questionText + "</div>"
// how do i access wrapperSelector member of FormRender when renderQuestion is called as higher-order function?
**$(this.wrapperSelector).append(questionWrapper)**
}
}
Client code
var q1= new Question("question1", "Text Box")
var form = new Form()
form.addQuestion(q1)
var formRenderer = new FormRenderer()
formRenderer.render(form)
The question is the same as the title. I have asked help for a specific example above with Javascript comments. Any help is appreciated.
eachis the higher-order-function, notrenderQuestion. Btw, this looks to me like inappropriate usage ofeach- just use a for-loop and you don't have those problems.