В моей магистральной функции все работает нормально, даже фильтр. но проблема в том, что всякий раз, когда я нажимаю на тип фильтра и переключаюсь на другой тип фильтра, он фильтрует существующие отфильтрованные данные, а не извлекает новые с сервера и фильтрует...
в случае, если я добавлю вызов выборки поверх моей функции фильтра, он извлечет все данные без фильтрации... как я могу это исправить?..?
мой код:
$(document).ready(function(){
var school = {};
school.model = Backbone.Model.extend({
defaults:{
name:'no name',
age:'no age'
}
});
school.collect = Backbone.Collection.extend({
model:school.model,
url:'js/school.json',
initialize:function(){
this.sortVar = "name"
}
});
school.view = Backbone.View.extend({
tagName:'div',
className:'member',
template:$('#newTemp').html(),
render:function(){
var temp = _.template(this.template);
this.$el.html(temp(this.model.toJSON()));
return this;
}
});
school.views = Backbone.View.extend({
el:$('#content'),
events:{
'click #newData' : 'newArrival',
'click #showName' : 'showByName',
'click #showAge' : 'showByAge'
},
initialize:function(){
_.bindAll(this);
this.collection = new school.collect;
this.collection.bind('reset', this.render);
this.collection.fetch();
this.childViews = [];
},
newArrival:function(){
this.collection.fetch(); //it works fine, i use this for update
},
showByName:function(){
// this.collection.fetch(); //but i can't do this, it removes filtered data..
this.sortVar = 'name';
var filterType = _.filter(this.collection.models, function(item){
return item.get('name') != '';
})
this.collection.reset(filterType); //resets without fetching (filtering from existing datas..)
},
showByAge:function(){
// this.collection.fetch(); //but i can't do this, it removes filtered data..
this.sortVar = 'age';
var filterType = _.filter(this.collection.models,function(item){
return item.get('age') != 0;
})
this.collection.reset(filterType); //resets without fetching (filtering from existing datas..)
},
render:function(){
_.each(this.childViews, function(old){
old.remove();
});
this.childViews = [];
var that = this;
_.each(this.collection.models, function(item){
that.renderItem(item);
});
},
renderItem:function(item){
var newItem = new school.view({model:item});
this.$el.append(newItem.render().el);
this.childViews.push(newItem);
}
});
var newSchool = new school.views;
});
заранее спасибо, также у меня есть еще 2 метода для добавления: сортировка имени и возраста при отображении данных.