骨干

2012-08-10 117 views
0

我想骨干

到目前为止现在实现与骨干一个非常基本的分页&过滤支持实施分页,我有这样的事情在我的Collection.url()函数:

url: function() { 
    var url = 'http://localhost:9000/wines'; 
    var query = ''; 

    if (this.filter) query += '&filter=' + this.filter; 
    if (this.order) query += '&order=' + this.order; 
    if (this.page) query += '&page=' + this.page.toString(); 
    if (this.len)  query += '&len=' + this.len.toString(); 

    if (query) url += '?' + query.substring(1); 

    return url; 
}, 

你的想法

的问题是,当我试图创建(POST)或更新(PUT)的项目,加上字符串也发送到浏览器...

是否有一些方法,在url函数中,检测我正在发布的操作并仅在获取(GET)数据时添加查询字符串参数?

或者你会推荐另一种方法?

回答

0

我发现下面的方式做到这一点

我只是重写获取方法,像这样:

initialize: function(options) { 

    this._fetch = this.fetch; 

    this.fetch = function() { 
    options = {}; 
    options.url = 'http://localhost:9000/wines' 

    data = {}; 
    if (this.filter) data.filter = this.filter; 
    if (this.order) data.order = this.order; 
    if (this.page) data.page = this.page; 
    if (this.len) data.len = this.len; 

    options.data = data; 

    return this._fetch(options); 
    } 
}, 

我couln't找到一种方法来避免保存_fetch变量,我试着用

return Backbone.Collection.fetch(options); return Backbone.Collection.prototype.fetch(options);

return Wines.fetch(options); return Wines.prototype.fetch(options);

但他们都不似乎工作...

- 编辑

发现它骨干页:

回报Backbone.Collection.prototype.fetch.call(这一点,选择);