gpt4 book ai didi

javascript - 访问 ember.js 中的另一个模型数据

转载 作者:行者123 更新时间:2023-11-28 01:53:59 25 4
gpt4 key购买 nike

我正在尝试构建一个小型 Assets 跟踪系统。我有很多 Assets ,我有很多标签。 Assets 有很多标签,反之亦然

我希望能够从列表中选择一个标签,并仅显示属于所选标签的资源。

我很难弄清楚如何让选择 View 显示标签列表。我感觉这和我的路线有关...

我正在尝试使用 this.controllerFor('tags').set('content', this.store.find('tag') 将标签数据传递到 Assets 路由中,但它似乎没有正确设置数据......

我还意识到我缺乏过滤列表的逻辑。

http://jsfiddle.net/viciousfish/g7xm7/

Javascript代码:

App = Ember.Application.create({
ready: function() {
console.log('App ready');
}
});

App.ApplicationAdapter = DS.FixtureAdapter.extend();

//ROUTER
App.Router.map(function () {
this.resource('assets', { path: '/' });
this.resource('tags', { path: '/tags' });
});

//ROUTES
App.AssetsRoute = Ember.Route.extend({
model: function () {
return this.store.find('asset');
},
setupController: function(controller, model) {
this._super(controller, model);
this.controllerFor('tags').set('content', this.store.find('tag') );
}
});

//Tags Controller to load all tags for listing in select view
App.TagsController = Ember.ArrayController.extend();

App.AssetsController = Ember.ArrayController.extend({
needs: ['tags'],
selectedTag: null
});


//MODEL
App.Asset = DS.Model.extend({
name: DS.attr('string'),
tags: DS.hasMany('tag')
});

App.Tag = DS.Model.extend({
name: DS.attr('string'),
assets: DS.hasMany('asset')
});

//FIXTURE DATA
App.Asset.FIXTURES = [
{
id: 1,
name: "fixture1",
tags: [1,2]
},
{
id: 2,
name: "fixture2",
tags: [1]
},
{
id: 3,
name: "fixture3",
tags: [2]
}];

App.Tag.FIXTURES = [
{
id: 1,
name: 'Tag1',
assets: [1,2]
},
{
id: 2,
name: 'Tag2',
assets: [1,3]
}];

带 mustache 的 HTML:

<body>
<script type="text/x-handlebars" data-template-name="assets">
{{view Ember.Select
contentBinding="controller.tags.content"
optionValuePath="content.id"
optionLabelPath="content.name"
valueBinding="selectedTag"
}}

<table>
<tr>
<td>"ID"</td>
<td>"Name"</td>
</tr>
{{#each}}
<tr>
<td>{{id}}</td>
<td>{{name}}</td>
</tr>
{{/each}}
</table>
</script>
</body>

最佳答案

在 Ember.Select 中,您有 contentBinding="controller.tags.content",您需要使用 controllers 而不是 controller。因为需要将引用的 Controller 添加到 Controller 属性中。在您的情况下,您在 AssetsController 中有 needs: ['tags'],因此在 Assets 模板中,您只需要使用 controllers.tags访问该实例。

这是更新后的选择:

{{view Ember.Select
contentBinding="controllers.tags.content"
optionValuePath="content.id"
optionLabelPath="content.name"
valueBinding="selectedTag"
prompt="Select a tag"
}}

为了能够过滤数据,您可以创建一个依赖于 selectedTag 的计算属性。并使用 selectedTag 值过滤内容。就像下面这样:

App.AssetsController = Ember.ArrayController.extend({
needs: ['tags'],
selectedTag: null,
assetsByTag: function() {
var selectedTag = this.get('selectedTag');
var found = [];
this.get('model').forEach(function(asset) {
return asset.get('tags').forEach(function(tag) {
if (tag.get('id') === selectedTag) {
found.pushObject(asset);
}
});
});
return found;
}.property('selectedTag')
});

在模板中,您在每个助手中引用该属性:

{{#each assetsByTag}}
<tr>
<td>{{id}}</td>
<td>{{name}}</td>
</tr>
{{/each}}

这是工作 fiddle http://jsfiddle.net/marciojunior/gqZj3/

关于javascript - 访问 ember.js 中的另一个模型数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19386351/

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