Reputation: 1
my input is in HTML file my button is here in js file. Im a beginner here please teach me. Im still confuse of backbone.js
var ListView = Backbone.View.extend({
el: $('body'),
events: {
'click button#add': 'addItem'
},
initialize: function () {
_.bindAll(this, 'render', 'addItem', 'appendItem');
this.collection = new List();
this.collection.bind('add', this.appendItem);
this.counter = 0;
this.render();
},
render: function () {
$(this.el).append("<button id='add' class='btn btn-primary clear'>Add Items</button>");
$(this.el).append("<ul class='ul_adjust'></ul>");
_(this.collection.model).each(function (item) {
appendItem(item);
}, this);
},
addItem: function () {
var item = new Item();
item.set({
part1: $("#name").val()
});
this.collection.add(item);
},
appendItem: function (item) {
var itemView = new ItemView({
model: item
});
$('ul', this.el).append(itemView.render().el);
},
});
did you get it? i remove some of not necessary but i think this will be inserted somewhere here.
Upvotes: 0
Views: 1058
Reputation: 76
Your events
is setup so that when you click the #add
button, it calls the addItem
function. So you just need to clear the textbox #name
in this same function.
I would simply do:
$("#name").val("");
at the end of your addItem
function, after this.collection.add(item);
so the value is only cleared when you no longer need it.
Upvotes: 1