gpt4 book ai didi

javascript - store.filter() 没有按预期工作 ember.js(尝试搜索模型)

转载 作者:行者123 更新时间:2023-11-30 17:26:12 25 4
gpt4 key购买 nike

我正在尝试在这里实现一个搜索系统,但我在使用 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);
});
}
}
}
});

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

在那个过程之上,它本身不是模型的对象,它是其他东西。

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

最佳答案

你只需要传入一个返回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}
*/

关于javascript - store.filter() 没有按预期工作 ember.js(尝试搜索模型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24208906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com