进行搜索/加载自定义结果到ember存储? [英] Doing a search/loading custom results into ember store?

查看:102
本文介绍了进行搜索/加载自定义结果到ember存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做了一些搜索和阅读ember数据的源代码,但我无法找出如何做到这一点。所以我的索引页加载数据如下所示:

Been doing some searching and reading the source code of ember-data but I'm having trouble figuring out how to do this. So my index page loads the data like so:

App.SaleRecordsRoute = Ember.Route.extend({
  setupController: function(controller) {
    controller.set('content', App.SaleRecord.find());
  }
});

在服务器上,我只需回复最新的10条记录。我也想做,是查询相同的URL,传递一个单一的搜索字段。根据数据库中的几个字段检查此搜索字段或条件。这些字段在连接语句中有两种不同的模型,所以我不认为我可以做:

On the server, i simply respond with the latest 10 records. What i also want to do, is query the same url, passing a single search field. This search field or criteria is checked against a few fields in the database. These fields are across a couple different models in the join statement, so i don't think i can just do:

App.SaleRecord.find({ book.title: critera });

书为belongsTo记录。

Book being the belongsTo record.

在该路由的控制器中,这是我至少查询数据的功能,它的工作原理如下:

In the controller for that route, here's what i have for querying the data at least, which works as expected:

App.SaleRecordsController = Ember.ArrayController.extend({
  search: function() {
    $.getJSON('/search?criteria=' + $('.criteria').val(), function(data) {

    });
  }
});

但我不知道从这里做什么。

But I'm unsure of what to do from here.


  1. 如果某些记录由于是最后10个结果的一部分而已经在商店中,我需要先卸载吗?

  2. 如何从json哈希将数据加载到商店?我知道如何创建一个记录并填充它,所以我可以循环访问数据,但我想知道是否有一个班轮。

感谢。我希望这个问题是清楚的:)

Thanks. I hope the question was clear :)

推荐答案

管理找到答案,主要是感谢这个问题/答案: href =https://stackoverflow.com/questions/15653525/ember-js-ajax-call-is-not-working-as-expected> Ember.js Ajax调用无法正常工作

Managed to find an answer, mainly thanks to this question/answer here: Ember.js Ajax call is not working as expected

我将ajax调用从控制器移动到模型函数中。所以在链接的问题中,我做了一个自定义函数来调用控制器:

I moved the ajax call out of the controller and into a model function. So like in the linked question, i made a custom function to call in the controller:

LocalBookFinder.SaleRecord = DS.Model.extend({
  isbn: DS.attr('string'),
  price: DS.attr('number'),
  condition: DS.attr('string'),
  book: DS.belongsTo('LocalBookFinder.Book')
});

LocalBookFinder.SaleRecord.reopenClass({
  search: function(criteria) {
    var result = Ember.ArrayProxy.create({content: []});
    var _this = this;
    $.getJSON('/search?criteria=' + criteria, function(data) {
      var ids = [];
      var arr = data.sale_records;
      for(var i = 0; i < arr.length; i++) {
        var sale_record = arr[i];
        if(ids.indexOf(sale_record.book_id) == -1) {
          ids.push(sale_record.book_id);
        }
      }

      var ajax_count = ids.length;
      var sale_records = [];
      for(var i = 0; i < ids.length; i++) {
        $.getJSON("/books/" + ids[i], function(data) {
          for(var j = 0; j < arr.length; j++) {
            var sale_record = arr[j];
            if(sale_record.book_id == data.book.id) {
              sale_record.book = data.book;
              sale_records.push(sale_record);
            }
          }
          if(i == ajax_count) {
            console.log(sale_records);
            result.set('content', sale_records);
          }
        });
      }
    });
    return result;
  }
});

然后在控制器中,我只需要:

Then in the controller, i simply do:

search: function() {
  var _this = this;
  var result = LocalBookFinder.SaleRecord.search($('.criteria').val());
  this.set('content', result);
}

我认为模型中搜索功能的第一个ajax位是相当直前锋。它请求数据,并设置一个空的ember数组代理来存储它。现在,由于我正在从一个子记录中加载图书和标题,所以我需要单独调用它们。所以在第一个结果集,我填充一个我需要请求的一组书签。通过每个ajax调用,我将查看销售记录数组,并将该book属性设置为生成的哈希数据。一旦完成(通过计数器),我将使用JSON数据设置结果内容。

I think the first ajax bit of the search function in the model is pretty straight forward. It requests the data, and sets an empty ember array proxy to store it in. Now, since i am loading the book image and title from a child record, i need to call those separately. So with the first result set, i populate an array of book ids that i need to request. With each ajax call, i go through the array of sale records, and set the book property to the resulting hash data. Once it's done (via a counter) i set the result content with the JSON data.

这篇关于进行搜索/加载自定义结果到ember存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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