[英]Load data from controller in ember.js
I want to implement a system that shows me the newest posts. 我想实现一个向我显示最新帖子的系统。 For this I do not want to use the index action from the user as this is already taken for another post function but a "newest" action.
为此,我不想使用来自用户的index操作,因为这已经用于另一个post函数,但是是“最新”操作。 It is showed on the index route with a
{{ render "postNewest" }}
call. 通过
{{ render "postNewest" }}
调用{{ render "postNewest" }}
其显示在索引路由上。 I would prefer to load the data in the PostNewestController or PostNewestView instead of the route for abstraction reasons. 由于抽象的原因,我希望将数据加载到PostNewestController或PostNewestView中,而不是路由中。 I tried two ideas to achieve this, but none worked so far:
为了达到这个目的,我尝试了两种方法,但到目前为止都没有奏效:
create a custom adapter and add a findNewest()
method: the findNewest()
method is sadly not found when trying to call in the init method of the controller. 创建一个自定义适配器并添加
findNewest()
方法:当尝试在控制器的init方法中调用时,找不到findNewest()
方法。
write the request directly into the init method and then update with store.loadMany(payload)
: data is successful request. 将请求直接写入init方法,然后使用
store.loadMany(payload)
更新:数据成功请求。 However, I do not know how to access the data from the template and set the content of the controller. 但是,我不知道如何从模板访问数据并设置控制器的内容。
Is there any way for this? 有什么办法吗?
EDIT: 编辑:
Here is the source code to better understand the problem: 这是可以更好地理解问题的源代码:
PostModel.js PostModel.js
App.Post.reopenClass({
stream: function(items) {
var result = Ember.ArrayProxy.create({ content: [] });
var items = [];
$.getJSON("/api/v1/post/stream?auth_token=" + App.Auth.get("authToken"), function(payload) {
result.set('content', payload.posts);
});
return result;
}
});
PostStreamController.js PostStreamController.js
App.PostStreamController = Ember.ArrayController.extend({
init: function() {
this.set("content", App.Post.stream());
},
});
index.hbs index.hbs
{{# if App.Auth.signedIn}}
{{ render "dashboard" }}
{{else}}
{{ render "GuestHeader" }}
{{/if}}
{{ render "postStream" }}
postStream.hbs postStream.hbs
{{#each post in model}}
<li>{{#linkTo 'post.show' post data-toggle="tooltip"}}{{post.name}}{{/linkTo}}</li>
{{else}}
Nothing's there!
{{/each}}
PostShowRoute.js PostShowRoute.js
App.PostShowRoute = Ember.Route.extend({
model: function(params) {
return App.Post.find(params.post_id);
},
setupController: function(controller, model) {
controller.set('content', model);
},
});
I Had this issue too. 我也有这个问题。 Just add
init
in your controller, and define the model you want to get there. 只需在控制器中添加
init
,然后定义要到达的模型。
In your Controller 在您的控制器中
App.PostRecentController = Ember.ArrayController.extend({
init: function() {
return this.set('content', App.Post.find({
recent: true
}));
}
});
In your template 在您的模板中
{{#each post in content}}
{{post.body}}
{{/each}}
I would recommend you check EMBER EXTENSION, it will give you a good idea of the naming, and see if everything is missing. 我建议您检查EMBER EXTENSION,它将为您提供一个很好的命名思路,并查看是否丢失了所有内容。
I figured out the problem. 我解决了这个问题。 The Post model has a belongsTo relationship to another model.
Post模型与另一个模型有一个belongsTo关系。 This relationship is not loaded by Ember so in the stream() method I have to load this manually.
这种关系不是由Ember加载的,因此在stream()方法中,我必须手动加载此关系。 Then everything works as expected.
然后一切都会按预期进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.