【问题标题】:How to fetch and filter the data如何获取和过滤数据
【发布时间】:2013-01-28 10:59:02
【问题描述】:

在我的主干功能中,一切正常,甚至是过滤器。但问题是,每当我点击过滤器类型并切换到另一个过滤器类型时,它都会从现有的过滤数据中过滤,而不是从服务器和过滤器中获取新数据...

如果我在我的过滤器函数上添加 fetch 调用,它会获取应用所有数据,而不过滤......我该如何解决这个问题......?

我的代码:

    $(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;
    });

提前谢谢,我还有另外两种方法可以添加,即在显示数据时对姓名和年龄进行排序。

【问题讨论】:

  • 我对 Backbone 不是很熟悉,但问题似乎是“school.collect”方法被记住了。因此,如果您不重新执行对 school.json 文件的调用,您仍然拥有旧数据。
  • 是的,但是当我回忆起 json 数据时,过滤器不起作用

标签: backbone.js backbone-views


【解决方案1】:

这对我有用.. 谢谢大家。

showByAge:function(){
        var that = this;
        this.sortVar = 'age';
        this.collection.fetch()
        .done(function(){ // i am proceeding after i finish the fetch!
            var filterType = _.filter(that.collection.models,function(item){
                return item.get(that.sortVar) != 0;
            })
            that.collection.reset(filterType);
        })
    },

【讨论】:

  • 对我有用,除了 fetch() 需要包装在 $.when()
  • 这仍然涉及最初通过 xhr 获取所有模型,但这是不必要的。
  • 谢谢!这是唯一有效的技巧,在使用 Obscura 时,您的技术不会崩溃,而不是执行 models.where({status: 'active'})
猜你喜欢
  • 2011-08-28
  • 1970-01-01
  • 2015-10-25
  • 2012-06-29
  • 2018-06-16
  • 1970-01-01
  • 2022-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多