store.filter() 未按预期工作 ember.js (尝试搜索模型)

2023-12-05

我正在尝试在这里实现一个搜索系统,但在 store.filter() 方面遇到了一些问题

首先我找不到任何好的文档store.filter()除了这里之外的方法:http://emberjs.com/guides/models/frequently-asked-questions/

因此,我使用该页面上提供的示例作为指导。

这就是我的代码中的内容

App.ApplicationController = Ember.ObjectController.extend({
    isDropdown: false,
    actions: {
        handle_search: function() {
            var search_text = this.get('search_text');
            if (search_text) {
                this.set('isDropdown', true);
                return this.store.filter('procedure', {proc_name: search_text}, function(procedure) {
                    console.log(procedure);
                });
            }
        }
    }
});

但是当我记录返回的内容时,它基本上返回每个模型。而不是没有结果或结果有限。

最重要的是procedure它本身不是模型的对象,而是其他东西。

所以我的问题是如何获得带有字段的实际模型以及如何确保商店实际上正在过滤结果?


您只需要传入一个返回 true/false 的函数即可包含记录

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return this.store.filter('color', function(item){
      return item.get('color')=='red';
    });
  }
});

http://emberjs.jsbin.com/OxIDiVU/647/edit

如果您想同时回调服务器(通过查询查找),您可以包含可选的查询参数,下面的示例将调用/colors?color=green

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return this.store.filter('color', {color:'green'}, function(item){
      return item.get('color')=='red';
    });
  }
});

/**
    Takes a type and filter function, and returns a live RecordArray that
    remains up to date as new records are loaded into the store or created
    locally.

    The callback function takes a materialized record, and returns true
    if the record should be included in the filter and false if it should
    not.

    The filter function is called once on all records for the type when
    it is created, and then once on each newly loaded or created record.

    If any of a record's properties change, or if it changes state, the
    filter function will be invoked again to determine whether it should
    still be in the array.

    Optionally you can pass a query which will be triggered at first. The
    results returned by the server could then appear in the filter if they
    match the filter function.

    Example

    ```javascript
    store.filter('post', {unread: true}, function(post) {
      return post.get('unread');
    }).then(function(unreadPosts) {
      unreadPosts.get('length'); // 5
      var unreadPost = unreadPosts.objectAt(0);
      unreadPost.set('unread', false);
      unreadPosts.get('length'); // 4
    });
    ```

    @method filter
    @param {String or subclass of DS.Model} type
    @param {Object} query optional query
    @param {Function} filter
    @return {DS.PromiseArray}
  */
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

store.filter() 未按预期工作 ember.js (尝试搜索模型) 的相关文章

  • 如何从一维数组和静态字符串创建对象

    我想要一个像 var obj ABC name true dob true CDE name true dob true EFG name true dob true CBA name true dob true XYZ name true
  • 使用 JavaScript 格式化日期

    JavaScript 中的日期格式有问题 这是我的函数代码 originalDate 2016 03 02 09 12 14 989522 var d new Date originalDate month d getMonth 1 day
  • 在 onclick 事件上请求麦克风

    有一天 我偶然发现了这个 Javascript 录音机的例子 http webaudiodemos appspot com AudioRecorder index html http webaudiodemos appspot com Au
  • javascript:全局变量泄漏

    每当我向 Firefox 提交插件时 我都会收到一封电子邮件 告诉我我的一些变量正在泄漏到全局范围内 一旦他们告诉我我解决了问题 但在那之前有什么方法 程序 来检查变量是否泄漏到全局范围内 Thanks Both JSLint http w
  • 如何使用 JavaScript 刷新页面?

    如何使用 JavaScript 刷新页面 Use location reload https developer mozilla org en US docs Web API Location reload 例如 每当元素带有以下内容时重新
  • 通过纯 JavaScript 获取 div 的第 n 个子元素

    我有一个名为 myDiv 的 div 元素 我怎样才能得到 myDiv 的第n个孩子DOM https en wikipedia org wiki Document Object Model操纵 Markup function reveal
  • HTML 和 标签有什么区别?

    HEAD 标签和 BODY 标签有什么区别 大多数 HTML 书籍仅 简短 提及 and 标签 但它们消失得很快 它们会影响浏览器呈现网页的方式吗 另外 它们会影响 javascript 的运行顺序吗 我的意思是 如果我里面有一个javas
  • 如何在 mongodb 聚合管道中使用 Javascript 对象?

    我有一个 JS 对象norm我想在 mongo 聚合管道中使用它 如下所示 var norm 1 1 2 1 16 3 1 413 4 1 622 5 1 6 6 1 753 7 3 001 8 2 818 9 3 291 10 2 824
  • 有什么方法可以复制 div 的渲染 HTML 吗?

    我正在开发电子邮件签名生成器 我想通过按按钮复制最终签名 而不是手动选择签名并将其复制到剪贴板 这意味着我需要它们的图像 文本和样式 我尝试了几种变体 包括 w3schools 的变体 但没有成功 其中一些只是复制文本 但没有样式 例子 h
  • React中如何触发同级组件的函数?

    I am new to front end world and could not figure out how to trigger a function from a sibling component Lets say I have
  • 调用类实例方法 onclick javascript

    我有一个 javascript 文件 其中包含包含方法函数的类 我想知道如何从 onClick 事件调用类实例方法 function MyClass this instanceData Display Me this DisplayData
  • 仅在 Chrome 上我收到此错误:Uncaught TypeError: Illegal constructor [关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 当我在 Chrome 上加载 jQuery 时 我会收到此错误 Uncaught TypeError Illegal constr
  • 修改 Twitter 帖子上可编辑 Div 的内容

    我正在编写一个 chrome 扩展 它可以帮助用户在 Twitter 上输入内容 当在 twitter 上写推文时 twitter 会打开一个可编辑的 div 容器 当用户输入内容时 twitter 大概正在使用某些网络框架 会生成子 di
  • 冒泡可用于图像加载事件吗?

    我可以用吗 window addEventListner 某种程度上来说 我所有的图像都有一个display none 图像加载后 我想设置display inline 这样我就可以规范下载图像时显示的内容 在这种情况下 我无法预加载图像
  • 如何按 Angular 表中的属性(该属性具有单个 rownspan)进行分组?

    我没有找到这个问题的合适标题 我的问题是 例如 我有一个包含两列的表 列汽车品牌和列汽车型号 我希望表是 like in this picture 换句话说 品牌名称只会出现 1 次 我的输入数组采用以下 json 格式 brand Aud
  • Aurelia 中的角度服务?

    我还没有找到详细说明如何从 Angular 1 x 迁移到 Aurelia 的详细文档 到目前为止 我只看到人们详细介绍了 Angular 的概念directive可以在 Aurelia 中使用重制 customElement 好吧 足够简
  • 绘制多边形

    我正在使用 Google Maps API V3 根据路径绘制多边形 该路径是随机未排序坐标点 LatLng 的数组 这会产生以下形状 Polylines intersect Problem 由于多边形的形状取决于路径中点的顺序 因此如何对
  • 将两个数字相加将它们连接起来而不是计算总和

    我将两个数字相加 但没有得到正确的值 例如 做1 2返回 12 而不是 3 我在这段代码中做错了什么 function myFunction var y document getElementById txt1 value var z do
  • $ 在 JQuery 中意味着什么

    在下面的 var obj one 1 two 2 three 3 four 4 five 5 each obj function i val console log val 这里是什么意思 是对象吗 是一个别名jQuery对象 函数 它充当
  • 为什么 try catch 块没有捕获 Promise 异常?

    我对承诺的错误处理感到困惑 答案可能很明显 但我不明白 我有以下示例代码 var test async function throw new Error Just another error try test then catch err

随机推荐