简体   繁体   English

灰烬附加相同的模型进行布线

[英]Ember appending same models to route

I'm having an issue where Ember continues to append the same route models to a route. 我遇到一个问题,其中Ember继续将相同的路由模型附加到路由。

# ROUTER
MediaUi.Router.map(function () {
    this.route('movie', { path: '/movie/:id' });
});

# ROUTE
MediaUi.IndexRoute = Ember.Route.extend({
  'model': function() {
    return MediaUi.Media.find();
  }
});

# MODEL
MediaUi.Media = DS.Model.extend({
    'type':     DS.attr('string'),
    'title':    DS.attr('string'),
    'director': DS.attr('string'),
    'year':     DS.attr('number'),
    'url':      DS.attr('string'),
    'media':    DS.attr('string'),
    '_id':      DS.attr('string'),
    'description': DS.attr('string', { 'defaultValue': 'TBD' })
});x

DS.RESTAdapter.map('MediaUi.Media', {
    'id':   { 'key': '_id' }
});

DS.RESTAdapter.reopen({
  url: 'http://localhost:3000'
});


#TEMPLATE
<table class="table">
    <thead>
        <tr>
            <th>Title</th>
            <th>Description</th>
            <th>Actions</th>
        </tr>
    </thead>
  {{#each item in model}}
    <tr {{ bindAttr data-id="item._id" }}>
        <td>{{#linkTo 'movie' item }} {{ item.title }} {{/linkTo}}</td>
        <td><p>{{ item.description }}</p></td>
        <td>
            <button {{ action "launchModal" item target="view" }} class="btn btn-info">More Information</button>
            <button class="btn btn-warning">Edit</button>
            <button class="btn btn-danger">Remove</button>
        </td>
    </tr>
  {{/each}}
</table>

{{ view MediaUi.ModalView }}

# JSON


{
  "medias": [
    {
      "type": "movie",
      "title": "The Dark Knight",
      "director": "Christopher Nolan",
      "year": 2008,
      "url": "http://www.imdb.com/title/tt0468569/",
      "media": "Blu-ray",
      "_id": "51e2fc1b4c19500de6000001"
    },
    {
      "type": "movie",
      "title": "Inception",
      "director": "Christopher Nolan",
      "year": 2010,
      "url": "http://www.imdb.com/title/tt1375666/",
      "media": "Blu-ray",
      "_id": "51e2fc1b4c19500de6000002"
    },
    {
      "type": "movie",
      "title": "The Dark Knight Rises",
      "director": "Christopher Nolan",
      "year": 2012,
      "url": "http://www.imdb.com/title/tt1345836/",
      "media": "Blu-ray",
      "_id": "51e2fc1b4c19500de6000003"
    },
    {
      "type": "movie",
      "title": "Django Unchained",
      "director": "Quentin Tarantino",
      "year": 2012,
      "url": "http://www.imdb.com/title/tt1853728/",
      "media": "Blu-ray",
      "_id": "51e2fc1b4c19500de6000004"
    },
    {
      "type": "movie",
      "title": "City of God",
      "director": "Kátia Lund,Fernando Meirelles",
      "year": 2002,
      "url": "http://www.imdb.com/title/tt0317248/",
      "media": "DVD",
      "_id": "51e2fc1b4c19500de6000005"
    }
  ]
}

This happens when the following is met: 满足以下条件时会发生这种情况:

  • User Transitione to Model specific page ('movie/:movie_id') 用户过渡到特定于模型的页面('movie /:movie_id')
  • User then clicks the back button in the browser or clicks on a back button in the application created with a linkTo handlebars helper 然后,用户单击浏览器中的“后退”按钮,或单击使用链接创建的应用程序中的“后退”按钮。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

If anyone comes across this issue, this might have to do with your backend. 如果有人遇到此问题,则可能与您的后端有关。 In my case it was the fact that MongoDB uses _id and not id as the identifier, and Ember associates id as a primary key - keeping track of your records. 在我的情况下,事实是MongoDB使用_id而不是id作为标识符,而Ember将id关联为主键-跟踪记录。 Therefore you need to create an adapter that serializes this difference and configure your application appropriately. 因此,您需要创建一个序列化此差异的适配器并适当地配置您的应用程序。

In my case I had to do the following: 就我而言,我必须执行以下操作:

MediaUi.Adapter = DS.RESTAdapter.extend({
    serializer: DS.RESTSerializer.extend({
    primaryKey: function(type){
      return '_id';
    }
  })
});

MediaUi.Store = DS.Store.extend({
  revision: 11,
  adapter: 'MediaUi.Adapter'
});

Reference: Ember, Ember Data and MongoDB's _id 参考: Ember,Ember数据和MongoDB的_id

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

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