在学习Ember.js时,我似乎陷入困境。
我可能会大大滥用这些组件,或者我错过了一些小事 - 无论哪种方式,请帮忙!
这就是我想要的:
arrangedContent
代替模型的原因)我有3个问题:
filterString
值keyPress
后面完成的,所以最初表是空的。在按下某个键之前,我该如何填充表呢?this.set('filteredItems', items)
的调用并不反映对表格的更改。我尝试使用propertyWillChange
和propertyDidChange
,但仍然没有运气。filterString
和filteredItems
会很好,但我怀疑这是不可能的。我想更多的是为了便于携带这是jsbin:http://emberjs.jsbin.com/qicuq/1/edit
模板
<script type="text/x-handlebars" data-template-name="components/filter-input">
{{input type="search" placeholder="Filter..." value=value}}
</script>
<script type="text/x-handlebars" data-template-name="index">
{{filter-input value=filterString}}
<br/>
<table border=1>
<tr>
<th>System Name</th>
<th>Owner</th>
</tr>
{{#each item in filteredItems}}
<tr>
<td>{{item.name}}</td>
<td>{{item.owner.username}}</td>
</tr>
{{/each}}
</table>
</script>
JS
App.IndexController = Ember.ArrayController.extend({
filterableProperties: ['name', 'owner.username'],
filteredItems: [],
filterString: null,
});
App.FilterInputComponent = Ember.Component.extend({
arrangedContentBinding: 'controller.arrangedContent',
filterStringBinding: 'controller.filterString',
filteredItemsBinding: 'controller.filteredItems',
filterablePropertiesBinding: 'controller.filterableProperties',
keyPress: function() {
var items = this.get('arrangedContent');
console.log('-------------');
console.log('items ' + items);
console.log('value ' + this.get('value'));
console.log('filterString ' + this.get('filterString'));
console.log('valueBinding ' + this.get('valueBinding'));
console.log('filterableProperties ' + this.get('filterableProperties'));
// filter 'items' here by the value of 'filter-input' and whether or not the property is in the filterableProperties list
console.log('filteredItems-before ' + this.get('filteredItems'));
//this.propertyWillChange('filteredItems');
this.set('filteredItems', items);
//this.propertyDidChange('filteredItems');
console.log('filteredItems-after ' + this.get('filteredItems'));
},
});
键入&#39; a&#39; 控制台输出进入输入框
"-------------"
"items [object Object],[object Object]"
"filterString null"
"value null"
"valueBinding Ember.Binding<ember274>(_parentView.context.filterString -> value)"
"filterableProperties name,owner.username"
"filteredItems-before "
"filteredItems-after [object Object],[object Object]"
谢谢你们,真的很感激任何帮助。
答案 0 :(得分:1)
我能够通过以下修改让您的搜索工作:
App.IndexController = Ember.ArrayController.extend({
filteredItems: function() {
var content = this.get("content");
var filterString = this.get("filterString");
if (filterString) {
return content.filter(function(item) {
return item.name.indexOf(filterString) === 0;
});
} else {
return content;
}
}.property("filterString"),
filterString: null
});
App.FilterInputComponent = Ember.Component.extend({
filterStringBinding: 'controller.filterString',
keyUp: function() {
this.set("filterString", this.get("value"));
}
});
这是JsBin fiddle!
我希望这能解决你的问题。