1

入力ボックスと追加ボタンがある非常に単純な ul を作成しようとしています。追加ボタンをクリックすると、入力からのテキストが ul に追加されます。

これは私のコードです:

HTML :

<body>
<input type="text" id="name">
<button id="add">Add</button>
<ul id="mylist"></ul>

JS:

$(function(){

var myCollection = Backbone.Collection.extend();

var myView = Backbone.View.extend({

    el:$('body'),

    tagName:'li',

    initialize : function(e){
        this.collection.bind("add",this.render,this);
    },

    events:{
        'click #add' : 'addfoo',
        'click .button':'removefoo'
    },

    addfoo:function(){
       var myname= $('#name').val();
       $('#name').val('');
       console.log('name entered: '+myname);
       this.collection.add({name:myname});
    },

    removefoo:function(){
             //need the code here 
    },

    render:function(){

        $('#mylist').empty();
        this.collection.each(function(model){
            $('#mylist').append('<li>'+model.get('name') + "<button class='button'>"+"delete"+"</button></li>");
        });

    }

});
var view = new myView({collection: new myCollection()});
    });

私の追加機能は機能していますが、削除のボタンをクリックすると、コレクションからどのモデルを削除する必要があるのか​​ 、私はそこで立ち往生しています。私を助けてください。

つまり、ボタンがクリックされたときに削除するモデルを取得するにはどうすればよいですか

ありがとうございました

4

2 に答える 2

4

バックボーンへのよりモジュール化されたアプローチが必要だと思います。説明させてください。

バックボーンは、コードを整理する方法です。1 つの Backbone ビューだけですべてを実行しても、あまり変わりません。

代わりに、実際に必要なビューを確認してください。

  • メインビュー
  • リストビュー
  • ListItemView

MainView次のようになります。

var MainView = Backbone.View.extend({

    el: 'body',

    initialize : function(options) {
       this.collection = new Backbone.Collection.extend({ url: '/items' });
    },

    events:{
    },

    render:function(){
        var listView = new ListView({ el: this.$("#myList") });
        listView.render();

        this.collection.fetch();

        return this;
    }
});

ListView

var ListView = Backbone.View.extend({

    tagName: 'ul',

    initialize : function(options) {
        _.bindAll(this, "render");

        this.collection.on("add", this.appendListItem, this);
    },

    events:{
    },

    render: function() {
        this.collection.each(this.appendListItem, this);

        return this;
    },

    appendListItem: function (model, collection, options) {
        var listItem = new ListItemView({ model: model});
        this.$el.append(listItem.render().el);
    }
});

ListItemView

var ListItemView = Backbone.View.extend({

    tagName: 'li',

    initialize : function(options) {
        _.bindAll(this, "render");

        this.model.on("destroy", this.remove, this);
    },

    events:{
        "click button": "delete"
    },

    render:function(){
        this.$el.text(this.model.get('name'));
        this.$el.append("<button class='button'>Delete</button>");

        return this;
    },

    delete: function (event) {
        this.model.destroy();
    }
});

メイン ビューを開始します。var view = new MainView().render();

于 2012-11-23T11:49:05.553 に答える
0

UIで各モデルをレンダリングするときに、どの要素が削除されるかを知るために、各li要素に一意のIDを割り当てる必要があります。

それ以外の

$('#mylist').append('<li>'+model.get('name') + "<button class='button'>"+"delete"+"</button></li>");

テンプレートを使用したほうがよいでしょう。アンダースコアのテンプレート を使用すると、IDの割り当てとlisの動的な作成が簡単になります。

モデルを削除するには:

removefoo:function(evt){
        var target = evt.target;
        //Parse the target id to get the model id and then delete it.
    }

バックボーンのすべてのコンポーネントがどのように組み合わされるかを理解するには、https://stackoverflow.com/questions/8782704/backbone-js-tutorialを参照してください。

于 2012-11-23T11:28:22.583 に答える