javascript - dynamically create input type='text' boxes then add values from each into Backbone.js array -
i wondering how create number of text boxes based on type='number' box...so adds 1 number box, text box field appended backbone.js view...and once enters values textboxes, add each value spot in backbone model array. here of code:
<label for='choices'># choices students</label> <input type='number' name='choices' step=1 /> initialize: function(opts) { this.model = new questioncreatemodel({ mode: null, choices: ['a', 'b', 'c'], question: "question goes here", maxchoices: 0, minchoices: 0, wordlimit: 0, charlimit: 0, }),
as can kind of see, want take input type='number' , load text boxes can assign values choices array in backbone model.
thanks help!
-stu
your code insufficient think.
first, need collection , model.
then create view, listens add, remove, change or reset events of collection. if that, view handle events , renders whatever should render.
myview = backbone.view.extend({ initialize : function() { this.collection = this.options.collection || new mycollection(); this.collection.on("add remove reset change", this.render, this) }, events : { "change [type='number']" : "numberchanged" }, numberchanged : function(ev) { var $el = $(ev.target || ev.srcelement); var model = $el.data("model"); model.set("selectedchoice", $el.val()); }, render : function() { this.$el.empty(); this.collection.each(function(model) { $("<yourinput>").data("model", model) .appendto(this.$el); }, this); } });
now model , collection
var mymodel = backbone.model.extend({ initialize : function() { this.on("change:selectedchoice", this.onchoicechanged, this); }, onchoicechanged : function(model,value) { // here know, value selected, // can collection, should create new model if (this.collection) this.collection.push(); // trigger "add" event , "view" react , rerender. } }); var mycollection = backbone.collection.extend({ model : mymodel });
Comments
Post a Comment