ul から名前を追加および削除する簡単なアプリケーションを開発しています。入力とボタンがあります。ボタンをクリックすると、入力内のテキストが ul に追加されます。私のコードは次のとおりです。
<input type="text" placeholder="Enter friend's name" id="input" />
<button id="add-input">Add Friend</button>
<ul id="friends-list">
</ul>
バックボーン コード :
<script>
$(function() {
FriendList = Backbone.Collection.extend({
initialize: function(){
this.bind("add", function( model,options ){
var id = ( model.collection.indexOf(model) );
view.render(model,id);
});
this.bind("remove",function(model){
alert("here");
});
}
});
FriendView = Backbone.View.extend({
tagName: 'li',
events: {
'click #add-input': 'getFriend',
'click .button': 'removeFriend'
},
initialize: function() {
this.friendslist = new FriendList;
_.bindAll(this, 'render');
},
getFriend: function() {
var friend_name = $('#input').val();
this.friendslist.add( {name: friend_name} );
},
removeFriend: function(){
var friend_index = $('.button').attr('id');
alert(friend_index);
this.friendslist.remove();
},
render: function( model,id ) {
$("#friends-list").append("<li>"+ model.get("name")+"<button class=button id="+id+">"+"delete"+"</button>"+"</li>");
$('#input').val('');
}
});
var view = new FriendView({el: 'body'});
});
</script>
私の問題、私が立ち往生している場所:
i)追加機能は問題なく実行されています。削除ボタンをクリックすると、removeFriend機能に移動しますが、コレクションとアラート(「ここ」)には移動しません。ii)削除ボタンをクリックしてliを削除/削除するためのコードを書くのを手伝ってください
ありがとうございました