使用搜索参数获取Backbone集合

sa1*_*125 21 search query-string backbone.js

我想用Backbone.js.实现一个搜索页面.搜索参数取自简单形式,服务器知道解析查询参数并返回结果的json数组.我的模型或多或少看起来像这样:

App.Models.SearchResult = Backbone.Model.extend({
    urlRoot: '/search'
});

App.Collections.SearchResults = Backbone.Collection.extend({
    model: App.Models.SearchResult
});

var results = new App.Collections.SearchResults();
Run Code Online (Sandbox Code Playgroud)

我希望每次执行时results.fetch(),搜索表单的内容也会随GET请求一起序列化.有没有一种简单的方法来添加它,或者我是以错误的方式执行它,并且可能应该手动编码请求并从返回的结果创建集合:

$.getJSON('/search', { /* search params */ }, function(resp){
    // resp is a list of JSON data [ { id: .., name: .. }, { id: .., name: .. }, .... ]
    var results = new App.Collections.SearchResults(resp);

   // update views, etc.
});
Run Code Online (Sandbox Code Playgroud)

思考?

jak*_*kee 84

Backbone.js使用参数获取回答了大部分问题,但我也在这里提了一些.

data参数添加到您的fetch呼叫中,例如:

var search_params = {
  'key1': 'value1',
  'key2': 'value2',
  'key3': 'value3',
  ...
  'keyN': 'valueN',
};

App.Collections.SearchResults.fetch({data: $.param(search_params)});
Run Code Online (Sandbox Code Playgroud)

现在你的调用url添加了你可以在服务器端解析的参数.


fgu*_*len 13

注意:代码已简化且未经过测试

我认为你应该拆分功能:

搜索模型

它是服务器端的合适资源.唯一允许的行动是CREATE.

var Search = Backbone.Model.extend({
  url: "/search",

  initialize: function(){
    this.results = new Results( this.get( "results" ) );
    this.trigger( "search:ready", this );
  }
});
Run Code Online (Sandbox Code Playgroud)

结果集

它只负责收集结果模型列表

var Results = Backbone.Collection.extend({
  model: Result
});
Run Code Online (Sandbox Code Playgroud)

搜索表单

你看,这个观点是使智能工作,听form.submit,创建一个新的Search对象,并将其发送到服务器是created.此created任务并不意味着搜索必须存储在数据库中,这是正常creation行为,但并不总是需要这样.在我们的例子中create,搜索意味着搜索DB以查找具体的寄存器.

var SearchView = Backbone.View.extend({
  events: {
    "submit form" : "createSearch"
  },

  createSearch: function(){
    // You can use things like this
    // http://stackoverflow.com/questions/1184624/convert-form-data-to-js-object-with-jquery
    // to authomat this process
    var search = new Search({
      field_1: this.$el.find( "input.field_1" ).val(),
      field_2: this.$el.find( "input.field_2" ).val(),
    });

    // You can listen to the "search:ready" event
    search.on( "search:ready", this.renderResults, this )

    // this is when a POST request is sent to the server
    // to the URL `/search` with all the search information packaged
    search.save(); 
  },

  renderResults: function( search ){
    // use search.results to render the results on your own way
  }
});
Run Code Online (Sandbox Code Playgroud)

我认为这种解决方案非常干净,优雅,直观且非常易于扩展.


sa1*_*125 6

找到一个非常简单的解决方案 - 覆盖url()集合中的函数:

App.Collections.SearchResults = Backbone.Collection.extend({

  urlRoot: '/search',

  url: function() {
    // send the url along with the serialized query params
    return this.urlRoot + "?" + $("#search-form").formSerialize();
  }
});
Run Code Online (Sandbox Code Playgroud)

希望这并不会让任何拥有比我更多Backbone/Javascript技能的人感到震惊.

  • 这里的DOM引用与Collection有一点联系.但我做了更丑陋的事情:) (3认同)

ale*_*kop 6

看来Backbone(或者jQuery)的当前版本会自动对data值进行字符串化,因此不再需要调用$.param.

以下行产生相同的结果:

collection.fetch({data: {filter:'abc', page:1}});
collection.fetch({data: $.param({filter:'abc', page:1})});
Run Code Online (Sandbox Code Playgroud)

查询字符串将是filter=abc&page=1.

编辑:这应该是一个评论,而不是回答.