使用 Ember.js 中安排的内容访问 child functions/properties
Accessing child functions/properties with arranged content in Ember.js
我不知道该怎么做。基本上我有一个 'post' 具有功能 'imageURL' 的控制器。这个函数只接受一个字符串(ex image_1.jpg),并在其前面加上根路径(ex images/image_1.png)。我遇到的问题是在 parent 控制器中,我只想显示 4 个最新的帖子。所以问题是我不能在安排的内容上使用"imageURL"。这是相关代码。
App.HomeController = Ember.ArrayController.extend({
itemController: 'post',
sortProperties: ['date'],
sortAscending: false,
topPosts: function(){
var content = this.get('arrangedContent');
if(content.length >= 4) content = content.splice(0, 3);
return content;
}.property('content')
});
App.PostController = Ember.Controller.extend({
title: Ember.computed.alias('model.title'),
body: Ember.computed.alias('model.body'),
postDate: Ember.computed.alias('model.postDate'),
imageName: Ember.computed.alias('model.imageName'),
imageUrl: function(){
var id = this.get('model.id');
var postImageRoot = "images/posts/" + id + "/";
var imageName = this.get('model.imageName');
return postImageRoot + imageName;
}.property('imageName')
});
App.Post = DS.Model.extend({
authorId: DS.attr('number'),
author: DS.belongsTo('author'),
title: DS.attr('string'),
body: DS.attr('string'),
snippet: DS.attr('string'),
postDate: DS.attr('date'),
imageName: DS.attr('string')
});
如有任何帮助,我们将不胜感激! :D 我已经搜索了一段时间,但没有找到任何特别像这样的东西。使用数组代理会有帮助吗?还是我缺少一些简单的东西?
我会将 imageUrl 属性 移至模型:
App.Post = DS.Model.extend({
...
imageName: DS.attr('string'),
imageUrl: Ember.computed('id', 'imageName', function() {
var id = this.get('id');
var postImageRoot = "images/posts/" + id + "/";
var imageName = this.get('imageName');
return postImageRoot + imageName;
})
});
我不知道该怎么做。基本上我有一个 'post' 具有功能 'imageURL' 的控制器。这个函数只接受一个字符串(ex image_1.jpg),并在其前面加上根路径(ex images/image_1.png)。我遇到的问题是在 parent 控制器中,我只想显示 4 个最新的帖子。所以问题是我不能在安排的内容上使用"imageURL"。这是相关代码。
App.HomeController = Ember.ArrayController.extend({
itemController: 'post',
sortProperties: ['date'],
sortAscending: false,
topPosts: function(){
var content = this.get('arrangedContent');
if(content.length >= 4) content = content.splice(0, 3);
return content;
}.property('content')
});
App.PostController = Ember.Controller.extend({
title: Ember.computed.alias('model.title'),
body: Ember.computed.alias('model.body'),
postDate: Ember.computed.alias('model.postDate'),
imageName: Ember.computed.alias('model.imageName'),
imageUrl: function(){
var id = this.get('model.id');
var postImageRoot = "images/posts/" + id + "/";
var imageName = this.get('model.imageName');
return postImageRoot + imageName;
}.property('imageName')
});
App.Post = DS.Model.extend({
authorId: DS.attr('number'),
author: DS.belongsTo('author'),
title: DS.attr('string'),
body: DS.attr('string'),
snippet: DS.attr('string'),
postDate: DS.attr('date'),
imageName: DS.attr('string')
});
如有任何帮助,我们将不胜感激! :D 我已经搜索了一段时间,但没有找到任何特别像这样的东西。使用数组代理会有帮助吗?还是我缺少一些简单的东西?
我会将 imageUrl 属性 移至模型:
App.Post = DS.Model.extend({
...
imageName: DS.attr('string'),
imageUrl: Ember.computed('id', 'imageName', function() {
var id = this.get('id');
var postImageRoot = "images/posts/" + id + "/";
var imageName = this.get('imageName');
return postImageRoot + imageName;
})
});