2013-08-03 78 views
3

listento僅觸發全局集合,而不是我在創建時傳遞給視圖的集合。Backbone.js`listento`未針對已過濾的集合進行觸發

例如:

var MyView = Backbone.View.extend({ 
    initialize: function() { 
    this.listenTo(notes, "add", this.test); // <--- THIS FIRES 
    this.listenTo(this.collection, "add", this.test); // <-- THIS DOES NOT FIRE 
    }, 

    test: function() { 
    console.log('model added to collection') 
    } 
}); 

我過濾的集合中通過像這樣在創建視圖時:

var notesFilteredByGroup = notes.filter_group(123); 
var myView = new MyView({ 
    collection: notesFilteredByGroup 
}); 

這裏是Notes集合:

Notes = Backbone.Collection.extend({ 
    url: '/notes', 

    model: Note, 

    filter_group: function(groupNum) { 
    filtered = this.filter(function(note) { 
     return note.get('groupNum') === groupNum; 
    }); 
    return new Notes(filtered); 
    } 
}); 

當我提交了一個新的筆記,它更新了全球收藏就好了。我如何告訴notesFilteredByIdthis.collection收聽增加的模型?

編輯:

添加請求的代碼,改變了一些變量名,使問題更加清晰

注提交代碼:

var AddNoteView = Backbone.View.extend({ 
    tagName: 'div', 

    template: _.template(AddNoteTemplate), 

    events: { 
    'click #noteSubmitButton': 'submit' 
    }, 

    initialize: function() { 
    this.render(); 
    }, 

    render: function() { 
    var template = this.template(this.model.toJSON()); 
    this.$el.html(template); 
    return this; 
    }, 

    submit: function(event) { 
    event.preventDefault(); 

    var newNote = { 
     text: $('#text').val(), 
     groupNum: $('#groupNum').val(), 
    }; 

    this.collection.create(newNote, {wait: true}); 
    } 
}); 

實例化AddNoteView:

var notes = new Notes; 
notes.fetch(); 
var addNoteView = new AddNoteView({ 
    collection: notes 
}); 
+0

你能告訴,你添加到集合 –

+0

什麼'這個代碼。 collection'對應於'AddNoteView' –

回答

3

我想你notes全球收集當初始化廁所像這樣的ks

var notes = new Notes(); 

所以這是傳遞給不同的意見。

但是當你過濾收集

var notesFilteredById = notes.filter_id(123); 

    ... 
    return new Notes(filtered); 

您在返回新鈔票集合..

這將創建不具有相同的綁定作爲全球notes.

一個新的實例

因此,爲了達到這個目的,您還必須明確地將創建的模型添加到已過濾的集合中。

像這樣的事情

// You need to pass that into the view 
var addNoteView = new AddNoteView({ 
    collection: notes, 
    filteredCollection : notesFilteredByGroup 
}); 

在視圖中,您需要明確地添加到其他集合

var AddNoteView = Backbone.View.extend({ 
    tagName: 'div', 

    template: _.template(AddNoteTemplate), 

    events: { 
    'click #noteSubmitButton': 'submit' 
    }, 

    initialize: function() { 
    this.listenTo(this.collection, 'add', addToCollection); 
    this.render(); 
    }, 

    render: function() { 
    var template = this.template(this.model.toJSON()); 
    this.$el.html(template); 
    return this; 
    }, 
    addToCollection : function(model) { 
     // Need to add to the other collection. 
     this.options.filteredCollection.add(model); 
    }, 
    submit: function(event) { 
    event.preventDefault(); 

    var newNote = { 
     text: $('#text').val(), 
     groupNum: $('#groupNum').val(), 
    }; 

    this.collection.create(newNote, {wait: true}); 
    } 
}); 
+0

謝謝你澄清這一點。我以某種方式認爲這些更改會自動推送到過濾後的集合中。 –

+0

@EvanEmolo ..很高興有幫助:) –

相關問題