1

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. It is showed on the index route with a {{ render "postNewest" }} call. I would prefer to load the data in the PostNewestController or PostNewestView instead of the route for abstraction reasons. 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.

  • write the request directly into the init method and then update with store.loadMany(payload): data is successful request. 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

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

App.PostStreamController = Ember.ArrayController.extend({
    init: function() {
        this.set("content", App.Post.stream());
    },
});

index.hbs

{{# if App.Auth.signedIn}}
    {{ render "dashboard" }}
{{else}}
    {{ render "GuestHeader" }}
{{/if}}

{{ render "postStream" }}

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

App.PostShowRoute = Ember.Route.extend({
    model: function(params) {
        return App.Post.find(params.post_id);
    },
    setupController: function(controller, model) {
        controller.set('content', model);
    },
});
4

2 回答 2

1

我也有这个问题。只需添加init您的控制器,并定义您想要到达的模型。

在你的控制器中

App.PostRecentController = Ember.ArrayController.extend({
  init: function() {
    return this.set('content', App.Post.find({
      recent: true
    }));
  }
});

在您的模板中

{{#each post in content}}
    {{post.body}}
{{/each}}

我建议您检查 EMBER EXTENSION,它会让您对命名有一个很好的了解,并查看是否缺少所有内容。

于 2013-09-03T11:00:58.573 回答
1

我解决了这个问题。Post 模型与另一个模型有 belongsTo 关系。这种关系不是由 Ember 加载的,所以在 stream() 方法中我必须手动加载它。然后一切都按预期工作。

于 2013-09-11T11:39:01.337 回答