内容未在刷新博客帖子上加载 - ember.js

时间:2014-08-17 07:08:54

标签: ember.js

我有点陷入困境,无法弄清楚为什么博客文章没有在刷新时重新加载。我知道它与App.PostRoute有关,但似乎看不出它有什么问题。

App.Router.map(function() {
    this.resource('about');
    this.resource('posts', function() {
        this.resource('post', { path: ':post_id' });
    });

});

App.PostsRoute = Ember.Route.extend({
    model: function() {
        return $.get('/posts').then(function(data) {
            return data.posts.map(function(post) {
                return post;
            });
        });
    }
});

App.PostRoute = Ember.Route.extend({
    model: function(params) {
      var posts = this.modelFor('posts');
      return posts.findBy('id', params.post_id);
    }
});

1 个答案:

答案 0 :(得分:1)

尝试了你的js代码,它似乎工作正常,所以我猜它可能是模板。

http://emberjs.jsbin.com/jivudadewihe/1/edit

<强> HBS

<script type="text/x-handlebars" data-template-name="posts">
    <ul>
    {{#each post in model}}
      <li>
      {{#link-to "post" post.id}}
      {{post.title}}
      {{/link-to}}
      </li>
    {{/each}}
    </ul>
    <hr/>
    {{outlet}}
  </script>
  <script type="text/x-handlebars" data-template-name="post">
    {{this.id}}<br/>
    {{this.title}}
  </script>

<强> JS

App = Ember.Application.create();


App.Router.map(function() {
    this.resource('about');
    this.resource('posts', function() {
        this.resource('post', { path: ':post_id' });
    });

});

App.IndexRoute = Ember.Route.extend({
  redirect:function(){
    this.transitionTo("posts");
  }
});

var postsData = [
  {id:"1",title:"post1"},
  {id:"2",title:"post2"},
  {id:"3",title:"post3"}
];

App.PostsRoute = Ember.Route.extend({
    model: function() {
        return $.get('').then(function(data) {
            return postsData.map(function(post) {
                return post;
            });
        });
    }
});

App.PostRoute = Ember.Route.extend({
    model: function(params) {
      var posts = this.modelFor('posts');
      return posts.findBy('id', params.post_id);
    }
});