Skip to content

Instantly share code, notes, and snippets.

@wb4r
Created February 25, 2016 14:55
Show Gist options
  • Save wb4r/2cd65d47a43e7385f6c8 to your computer and use it in GitHub Desktop.
Save wb4r/2cd65d47a43e7385f6c8 to your computer and use it in GitHub Desktop.
backbone collection selection
App.ResultView = Marionette.ItemView.extend({
template: "#js-result",
})
App.ResultsView = Marionette.CompositeView.extend({
tagName: "ul",
// className: ".suggestions",
childView: App.ResultView,
template: "#js-results",
// this.listenTo("show", this.filter()),
filter: function() {
console.log("filtering!!!!!!!!!!!!!!!!!!!!!!!");
}
})
App.InputView = Marionette.ItemView.extend({
el: "#inputSearch",
initialize: function() {
_.bindAll(this, 'launchCollection')
},
events: {
"keyup": "launchCollection",
},
launchCollection: function(e) {
this.allBackToUnselected();
var array = [],
filtered = this.getCollectionMatches(array);
this.changeModelsToSelected(filtered)
console.log(App.FullList.where({"selected": true}));
var listview = new App.ResultsView({
collection: App.FullList,
filter: function(child) {
return App.FullList.where({"selected": true})
}
});
console.log(listview);
// var listview = new App.ResultsView({collection: App.FullList});
App.regions.list.show(listview)
},
getCollectionMatches: function(array) {
var search = $("#inputSearch").val();
_.each(App.FullList.models, function(model) {
if (model.attributes.city.toLowerCase().includes(search)) {
array.push(model.attributes.id)
}
})
return array;
},
changeModelsToSelected: function(filtered) {
_.each(filtered, function(id) {
var model = App.FullList.get(id)
model.set({"selected": true})
})
},
allBackToUnselected: function() {
App.FullList.invoke('set', {"selected": false});
}
})
var inputview = new App.InputView()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment