Permalink
Browse files

Added support for editing basic forms

  • Loading branch information...
1 parent 482b483 commit c2a35b5bc1600cc9f91543eb783dd106614b538a @mikeymckay committed Feb 4, 2012
@@ -15,8 +15,12 @@ class Router extends Backbone.Router
$("#content").html("<img src='images/coconut_logo_hori_1_med.jpg'/>")
$("#content").empty()
+ editQuestion: (question_id) ->
+ Coconut.designView ?= new DesignView()
+ Coconut.designView.render()
+ Coconut.designView.loadQuestion question_id
+
deleteQuestion: (question_id) ->
- console.log Coconut.questions.get(question_id)
Coconut.questions.get(question_id).destroy
success: ->
Coconut.menuView.render()
View
@@ -1,15 +1,17 @@
-var Coconut, Router,
- __hasProp = Object.prototype.hasOwnProperty,
- __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
-
-Router = (function(_super) {
-
- __extends(Router, _super);
-
+var Coconut, Router;
+var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
+ for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
+ function ctor() { this.constructor = child; }
+ ctor.prototype = parent.prototype;
+ child.prototype = new ctor;
+ child.__super__ = parent.prototype;
+ return child;
+};
+Router = (function() {
+ __extends(Router, Backbone.Router);
function Router() {
Router.__super__.constructor.apply(this, arguments);
}
-
Router.prototype.routes = {
"design": "design",
"select": "select",
@@ -22,22 +24,26 @@ Router = (function(_super) {
"manage": "manage",
"": "default"
};
-
Router.prototype["default"] = function() {
$("#content").html("<img src='images/coconut_logo_hori_1_med.jpg'/>");
return $("#content").empty();
};
-
+ Router.prototype.editQuestion = function(question_id) {
+ var _ref;
+ if ((_ref = Coconut.designView) == null) {
+ Coconut.designView = new DesignView();
+ }
+ Coconut.designView.render();
+ return Coconut.designView.loadQuestion(question_id);
+ };
Router.prototype.deleteQuestion = function(question_id) {
- console.log(Coconut.questions.get(question_id));
return Coconut.questions.get(question_id).destroy({
success: function() {
Coconut.menuView.render();
return Coconut.router.navigate("manage", true);
}
});
};
-
Router.prototype.manage = function() {
return Coconut.questions.fetch({
success: function() {
@@ -48,9 +54,11 @@ Router = (function(_super) {
}
});
};
-
Router.prototype.newResult = function(question_id) {
- if (Coconut.questionView == null) Coconut.questionView = new QuestionView();
+ var _ref;
+ if ((_ref = Coconut.questionView) == null) {
+ Coconut.questionView = new QuestionView();
+ }
Coconut.questionView.result = new Result({
question: question_id
});
@@ -63,9 +71,11 @@ Router = (function(_super) {
}
});
};
-
Router.prototype.editResult = function(result_id) {
- if (Coconut.questionView == null) Coconut.questionView = new QuestionView();
+ var _ref;
+ if ((_ref = Coconut.questionView) == null) {
+ Coconut.questionView = new QuestionView();
+ }
Coconut.questionView.result = new Result({
_id: result_id
});
@@ -82,26 +92,29 @@ Router = (function(_super) {
}
});
};
-
Router.prototype.design = function() {
+ var _ref;
$("#content").empty();
- if (Coconut.designView == null) Coconut.designView = new DesignView();
+ if ((_ref = Coconut.designView) == null) {
+ Coconut.designView = new DesignView();
+ }
return Coconut.designView.render();
};
-
Router.prototype.showResults = function(question_id) {
- var rowTemplate;
+ var rowTemplate, _ref;
$("#content").html(" <h1>" + question_id + "</h1> <a href='#new/result/" + question_id + "'>Start new result</a> <h2>Partial Results</h2> <table class='notComplete'> <thead><tr> <th>Name</th> <th></th> <th></th> </tr></thead> </table> <h2>Complete Results</h2> <table class='complete'> <thead><tr> <th>Name</th> <th></th> <th></th> </tr></thead> </table> ");
rowTemplate = Handlebars.compile(" <tr> <td>{{toShortString}}</td> <td><a href='#edit/result/{{id}}'>Edit</a></td> <td><a href='#view/result/{{id}}'>View</a></td> </tr> ");
- if (Coconut.resultCollection == null) {
+ if ((_ref = Coconut.resultCollection) == null) {
Coconut.resultCollection = new ResultCollection();
}
return Coconut.resultCollection.fetch({
success: function() {
return Coconut.resultCollection.each(function(result) {
return result.fetch({
success: function() {
- if (result.question() !== question_id) return;
+ if (result.question() !== question_id) {
+ return;
+ }
if (result.get("complete") === true) {
return $("table.complete").append(rowTemplate(result));
} else {
@@ -113,7 +126,6 @@ Router = (function(_super) {
}
});
};
-
Router.prototype.startApp = function() {
Coconut.questions = new QuestionCollection();
Coconut.questionView = new QuestionView();
@@ -122,13 +134,8 @@ Router = (function(_super) {
Coconut.menuView.render();
return Backbone.history.start();
};
-
return Router;
-
-})(Backbone.Router);
-
+})();
Coconut = {};
-
Coconut.router = new Router();
-
-Coconut.router.startApp();
+Coconut.router.startApp();
@@ -1,5 +1,6 @@
class DesignView extends Backbone.View
initialize: ->
+ @question = new Question()
el: $('#content')
@@ -56,15 +57,32 @@ class DesignView extends Backbone.View
"click #design-view button:contains(Save)" : "save"
save: ->
- question = new Question()
- question.loadFromDesigner $("#questions")
- question.save null,
+ @question.loadFromDesigner $("#questions")
+ @question.save null,
success: ->
Coconut.menuView.render()
add: (event) ->
- type = $(event.target).prev().val()
- id = Math.ceil(Math.random()*1000)
+ @addQuestion
+ type : $(event.target).prev().val()
+
+ loadQuestion: (questionId) ->
+ @question = new Question
+ id: questionId
+ @question.fetch
+ success: =>
+ $('#rootQuestionName').val @question.id
+ _.each @question.questions(), (question) =>
+ @addQuestion question.attributes
+
+ addQuestion: (options) ->
+ alert "Support for editing grouped forms not yet implemented" if options.questions
+ type = options.type
+ id = options.id || Math.ceil(Math.random()*1000)
+ label = options.label || ""
+ repeatable = options.repeatable || ""
+ selectOptions = options["select-options"] || "option1,option2"
+
if $("#questions").children().length > 0
$("#questions").append "
<button class='advanced' title='group'><img src='images/group.png'/></button>
@@ -78,12 +96,12 @@ class DesignView extends Backbone.View
</div>
<div>Type: #{type}</div>
<label for='label-#{id}'>Label</label>
- <input type='text' name='label-#{id}' id='label-#{id}'></input>
+ <input type='text' name='label-#{id}' id='label-#{id}' value='#{label}'></input>
"
if type is "select"
result += "
<label for='select-options-#{id}'>Select Options</label>
- <textarea name='select-options-#{id}' id='select-options-#{id}'>option1,option2</textarea>
+ <textarea name='select-options-#{id}' id='select-options-#{id}'>#{selectOptions}</textarea>
"
result += "
@@ -163,9 +181,8 @@ class DesignView extends Backbone.View
return $('#questions').children()
toHTMLForm: ->
- question = new Question()
- question.loadFromDesigner($("#questions"))
- questionView = new QuestionView(model: question)
+ @question.loadFromDesigner($("#questions"))
+ questionView = new QuestionView(model: @question)
questionView.toHTMLForm()
dump: ->
Oops, something went wrong.

0 comments on commit c2a35b5

Please sign in to comment.