Backbone.js的requestPager有条件排除网址paramater [英] Backbone.js requestPager conditionally exclude paramater from URL

查看:118
本文介绍了Backbone.js的requestPager有条件排除网址paramater的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RequestPager送入server_api所有属性的要求作为查询字符串。但是,有时我想排除在某些条件下的参数。这是怎么回事,我设置帕拉姆:

  server_api:{
        查询:功能(){
            返回this.searchQuery
        },
        类型:函数(){返回this.searchType}
}

如果 this.searchQuery 是空的,它使像查询=放大器的URL;类型= 1 。但我不希望发送查询键入时,它是空的,或者当我的其他一些条件失败

我知道肮脏的方式,如:

 如果(!myCollection.searchQuery){
    删除(myCollection.server_api.licensed);
}

但是,这不是维护。由于文本一次我打造这个功能。所以,我正在寻找这样做的更好的方法。任何帮助吗?


解决方案

如果你看的如何 server_api 用于

  _。每个(_。结果(自我server_api),功能(值,键){
  如果(_.isFunction(值)){
    值= _.bind(价值,个体经营);
    值=值();
  }
  queryAttributes [关键] =价值;
});

你会看到它使用 _。结果


  

结果 _。结果(对象属性)


  
  

如果指定属性的值是那么功能调用它;
  否则,返回它。

  VAR对象= {奶酪:松饼,东西:函数(){返回'废话'; }};
_.result(对象,'奶酪');
= GT; 松饼
_.result(对象,'东西');
= GT; 废话


这意味着,你可以让 server_api 返回适当的对象的功能。

RequestPager sends all the attributes in server_api to the request as query string. However, sometime I want to exclude a parameter on some condition. This is how, i'm setting the param:

server_api: {
        query: function () {
            return this.searchQuery
        },
        type: function(){ return this.searchType }
}

If this.searchQuery is empty, it makes the URL like ?query=&type=1. But I don't want to send query or type when it's empty or when my some other condition fails.

I know the dirty way like:

if(!myCollection.searchQuery){
    delete(myCollection.server_api.licensed);
}

But this is not maintainable. Because text time I've to create this function. So, I'm looking for a better way of doing this. Any Help?

解决方案

If you look at how server_api is used:

_.each(_.result(self, "server_api"), function(value, key){
  if( _.isFunction(value) ) {
    value = _.bind(value, self);
    value = value();
  }
  queryAttributes[key] = value;
});

you'll see that it uses _.result:

result _.result(object, property)

If the value of the named property is a function then invoke it; otherwise, return it.

var object = {cheese: 'crumpets', stuff: function(){ return 'nonsense'; }};
_.result(object, 'cheese');
=> "crumpets"
_.result(object, 'stuff');
=> "nonsense"

That means that you can make server_api a function which returns the appropriate object.

这篇关于Backbone.js的requestPager有条件排除网址paramater的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆