简体   繁体   English

Ember JS-无法将多个模型加载到具有归属关系的路由中

[英]Ember JS - Having trouble loading multiple models into a route with belongsTo relationship

I'm stuck in this situation where I cant set up my data correctly. 在这种情况下,我无法正确设置数据。 I want to be able to load all my posts, and have the author data load with each post as well. 我希望能够加载我的所有帖子,并在每个帖子中加载作者数据。 These are my models. 这些是我的模特。

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'),

    postSnippit: Ember.computed();
    imageUrl: Ember.computed()})
});


App.Author = DS.Model.extend({
    name:        DS.attr('string'),
    imageName:   DS.attr('string'),
    posts:       DS.hasMany('post')
});

and heres the relevant part of my router... 这是我路由器的相关部分...

App.Router.map(function() {
    this.route('blog', {path: '/blog'}, function() {
        this.resource('posts', { path: '/' });
        this.resource('post', {path: '/:id'});
    });
});

I have it setup correctly for a single post like so ... 我为单个帖子正确设置了它,就像...

App.PostRoute = Ember.Route.extend({
    setupController: function(controller, model){
        this._super(controller, model);
        controller.set('post', model);
        controller.set('author', this.get('author'));
    },
    afterModel: function(){
        Ember.run.scheduleOnce('afterRender', this, scrollToNav);
        var self = this;
        var id = this.get('model.authorId');
        return this.store.find('author', id).then(function(result){
            self.set('author', result);
        });
    },
    model: function(params){
        return this.store.find('post', params.id);
    }
});

So what happens now, is I am loading a list of all posts as the model for 'blog'. 所以现在发生的是,我正在加载所有帖子的列表作为“博客”的模型。 The route has a sidebar on the left with a list of all the blog posts, and an outlet on the right side that defaults to loading the 'posts' route. 该路线的左侧有一个侧栏,其中包含所有博客文章的列表,而右侧的出口则默认为加载“文章”路线。 I need the same info for both routes, a list of all posts, as well as the author information. 我需要两条路线的相同信息,所有帖子的列表以及作者信息。 The outlet loads the current post and replaces the 'posts' template, but retains the list of posts on the left sidebar. 出口加载当前帖子并替换“帖子”模板,但保留左侧栏中的帖子列表。 Currently I don't have the JSON data sideloaded because I couldn't figure it out either, so right now the data comes like this... 目前我没有侧面加载JSON数据,因为我也无法弄清楚,所以现在数据像这样...

{
    "posts": [
        {
            "id": "1",
            "authorId": "1",
            "title": "Title 1",
            "body": "This is the body 1",
            "uploadDate": "2015-06-03 19:26:15",
            "imageName": "image1.jpg"
        },
        {
            "id": "2",
            "authorId": "2",
            "title": "Title 2",
            "body": "This is the body 2",
            "uploadDate": "2015-06-03 19:26:15",
            "imageName": "image2.jpg"
        }
    ]
}

and then there is a separate call for authors... 然后有一个单独的呼吁作者...

{
    "authors": [
        {
            "id": "1",
            "name": "John Smith",
            "email": "jsmith@gmail.com",
            "imageName": "image1.jpg",
            "gender": "M",
            "bio": "John Smith is an awesome dude who went to awesome school!",
            "facebookUrl": "null",
            "twitterUrl": null,
            "linkedinUrl": null
        }
    ]
}

And now here are my current route objects (after going back and forth through 1million things). 现在这是我当前的路线对象(经过一百万次往返后)。

App.BlogRoute = Ember.Route.extend({
    model: function() {
        var store = this.store;
        var posts = store.findAll('post');
        return posts;

        /* *********************************
        **Tried this, keep getting an error saying 'content.slice' is not a function**
        *
        var authors = store.findAll('author');

         return Ember.RSVP.hash({
            posts: posts,
            authors: authors
        }).then(function(hash){
            return hash;
        }, function(reason){
            console.log(reason);
        });
        ********************************* */

    },
    setupController: function(controller, model) {
        this._super(controller, model);
        controller.set('model', model);
        controller.set('authors', this.get('authors')); //This doesnt do much, and I dont think its necessary.

        /* *********************************
        **Also tried this with the RSVP.hash function, would not render because of 'content.slice' error**

        controller.set('model', model.posts);
        controller.set('authors', model.authors)
        ********************************* */

    },
    afterModel: function(model) {
        Ember.run.scheduleOnce('afterRender', this, scrollToNav);
        var self = this;
        return this.store.findAll('author').then(function(result){
            self.set('authors', result);

        });
    }
});

App.PostsRoute = Ember.Route.extend({
    afterModel: function() {
        Ember.run.scheduleOnce('afterRender', this, scrollToNav);
    },
    model: function() {
        return this.modelFor('blog');
    }
});

The current behavior that happens, is that everything loads in the template, except for the author information. 当前发生的行为是,除了作者信息之外,所有内容都加载到模板中。 It only loads AFTER the actual post has been visited (ex: 'posts/1'), and then from then on, if I visit the posts or blog route, the author name will stay there. 仅在访问了实际帖子(例如:“ posts / 1”)之后加载,然后从此以后,如果我访问帖子或博客路线,作者姓名将停留在该位置。

I can't figure out how to make the information display all the time. 我不知道如何一直显示信息。 From what I can see in the ember inspector, the data for both is being loaded! 根据我在余烬检查器中看到的内容,两者的数据都已加载! Ik there is probably something simple I'm missing. 可能我缺少一些简单的东西。 Please let me know if you guys have a solution. 请让我知道你们是否有解决方案。

There is also a secondary problem that i'm sure is somehow linked. 我敢肯定还有一个次要问题已链接。 No matter what method I choose to set data to the controller, (ex: controller.set('posts', posts) I cannot reference that data from the template in an each loop by calling it the given name. (ex: {{#each posts as |post|}} should be the correct format}} Please correct me if that's not the correct way, or even what is the intended way to do it. 无论我选择哪种方法将数据设置到控制器,(例如:controller.set('posts',posts),我都无法通过在每个循环中调用给定名称来引用模板中的数据。(例如:{{ #每个帖子的| post |}}格式都应该正确}}如果这不是正确的方法,甚至是预期的操作方法,请纠正我。

And just for reference, here is the relevant parts of the templates for blogs, posts, and post. 仅供参考,以下是博客,帖子和帖子模板的相关部分。

<script type="text/x-handlebars" id="blog">
    <div class="container-fluid">
        <h3>Posts</h3>
        <ul>
            {{#each model as |post|}}
            <li>
                {{#link-to 'post' post}}
                <h3>{{post.title}}</h3>
                <h4>{{post.author.name}}</h4>
                <h5>{{format-date post.date}}</h5>
                {{/link-to}}
            </li>
            {{else}}
                No Models Loaded!
            {{/each}}
        </ul>
    </div>
    <div class="container-fluid">
        {{outlet}}
    </div>
</script>

<script type="text/x-handlebars" id="posts">
    <h1>Recent Blog Posts</h1>
    {{#each model as |post|}}
    <h1>{{post.title}}</h1>
    <h4>By: {{post.author.name}} </h4>
    <h4>Posted: {{format-date post.date}} </h4>

    <img src={{post.imageUrl}} class="img-responsive img-thumbnail">

    <p class="indent">
        {{post.body}}
    </p>

    {{#link-to 'post' post}}Read More{{/link-to}}
    {{/each}}
</script>

<script type="text/x-handlebars" id="post">
    {{#link-to 'posts' class="pull-right"}}<span class="glyphicon glyphicon-chevron-left"></span>Back{{/link-to}}

    <h1>{{post.title}}</h1>
    <h4>Written By: {{author.name}} </h4>
    <h4>Posted: {{format-date post.date}} </h4>
    <hr/>
    <div class="img-container center">
        <img src={{post.imageUrl}} class="img-responsive img-thumbnail">
    </div>
    <br/>
    <p class="indent">
        {{post.body}}
    </p>
</script>

Note: I tried a few different versions of Ember, and no matter what version I use I get the "content.slice is not a function" error, so I'm sure something I' m doing is wrong, but here is some extra info just incase :D! 注意:我尝试了几种不同版本的Ember,无论使用什么版本,都会收到“ content.slice不是函数”错误,因此,我确定我做的事情是错误的,但是这里有些额外的内容信息只是以防万一:D!

VERSION INFO:

Ember Inspector:      1.8.1
Ember:                1.13.0-beta.2+76473dd3
Ember Data:           1.0.0-beta.18
jQuery:               2.1.4

1) I recommend you should not use store.findAll , since this is private api method. 1)我建议您不要使用store.findAll ,因为这是私有api方法。 api link . api链接 The right way is to use store.find('modelName') instead. 正确的方法是改用store.find('modelName') For example, 例如,

model: function() {
  return this.store.find('post'); // promise to find all posts
}

2) If you want to load all posts and all authors in a route (for example in blog route) 2)如果您要加载路线中的所有帖子和所有作者(例如博客路线)

App.BlogRoute = Ember.Route.extend({

  model: function() {
    return Ember.RSVP.hash({
      posts: this.store.find('post'),
      authors: this.store.find('author')
    });
  },

  setupController: function(controller, modelHash) {
    controller.setProperties(modelHash);
    // or directly 
    // controller.set('posts', modelHash.posts);
    // controller.set('authors', modelHash.authors);
    // even
    // this.controllerFor('posts').set('authors', modelHash.authors);
  }
}); 

3) I'd simplify post route code: 3)我会简化后路由代码:

App.PostRoute = Ember.Route.extend({
  model: function(params){
    return this.store.find('post', params.id);
  },

  afterModel: function(model){
    var self = this;
    return model.get('author').then(function(author) {
        self.controllerFor('post').set('author', author);
    });
  }
  // setupController not needed here
});

4) You may need more steps. 4)您可能需要更多步骤。 Jsbin would be helpful to give accurate answer. Jsbin将有助于给出准确的答案。

UPD 08/jun/15: Looks like findAll will become public soon https://github.com/emberjs/data/pull/3234 . UPD 08 / jun / 15:好像findAll即将公开https://github.com/emberjs/data/pull/3234

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM