Ember数据侧面加载的属性被丢弃在模型上 [英] Ember Data sideloaded properties being dropped on a model

查看:81
本文介绍了Ember数据侧面加载的属性被丢弃在模型上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用


  • Ember RC3

  • Ember Data Revision 12

  • Handlebars RC3

我在许多模型中都有Ember Data侧向关系,以便我可以模板侧负的关系如下所示:

I have Ember Data sideloading relationships on many of my models, so that I can template the sideloaded relationships like so:

// Models
App.Client = DS.Model.extend({
    company: DS.attr('string'),
    accountNumber: DS.attr('string'),
    startDate: DS.attr('mysqlDate'),

    // Relationships
    campaigns: DS.hasMany('App.Campaign'),
    users: DS.hasMany('App.User'),
    phones: DS.hasMany('App.Phone'),
    addresses: DS.hasMany('App.Address')
});

App.User = DS.Model.extend({
    email: DS.attr('string'),
    password: DS.attr('string'),

    // Relationships
    userType: DS.belongsTo('App.UserType'),
    role: DS.belongsTo('App.Role'),
    clients: DS.hasMany('App.Client'),
    phones: DS.hasMany('App.Phone'),
    addresses: DS.hasMany('App.Address')
});

<!-- template -->
<script type="text/x-handlebars" data-template-name="user/index">
  <h2>{{email}}</h2>
  <h5>Clients</h5>
  <ul>
    {{#each client in model.clients}}
      <li>{{client.company}}</li>
    {{/each}}
  </ul>
</script>

这个功能奇妙,除了每10次重新加载一次。每隔一段时间,负载关系(在这种情况下, hasMany 关系 model.clients )不渲染到模板,而所有其他模型属性(而不是关系)都会呈现给模板。有什么奇怪的是,它只是在一段时间内每次都这样做。

This works wonderfully...except for every 1 in 10 reloads or so. Every once in a while the sideloaded relationship (in this case the hasMany relationship model.clients) DOES NOT render to the template while all other model properties (not relationships) DO render to the template. What's weird is that it only does this every once in a while.

我还不确定如何为这个问题设置一个js小提琴,所以我想要问:

I'm not quite sure yet how I can set up a js fiddle for this problem, so I wanted to ask:

在调用堆栈中,我可以设置一个断点来查看实际可以渲染的属性。

Where in the call stack could I set a break point to see what properties will actually get rendered?

我在相关模板中使用 {{debugger}} ,我只是不知道最好的地方是检查应用程序状态调用堆栈。

I'm using {{debugger}} in the template in question, I'm just not sure where the best place would be to inspect the application state in the call stack.

推荐答案

所以,我的问题是双重的。 第一个问题:以下是路由器地图和路由:

So, my problem was two-fold. First Problem: Here's my router map and routes:

App.Router.map(function() {
  this.resource('users', function() {
    this.route('create');
    this.resource('user', { path: ':user_id' }, function() {
      this.route('edit');
      this.route('delete');
    });
  });
});

App.UsersRoute = Ember.Route.extend({
  model: function() {
    return App.User.find();
  }  
});

// Default for this route
App.UserRoute = Ember.Route.extend({
  model: function(params) {
    return App.User.find(params.user_id);
  }
});

因此,当导航到路由'clients / 3' DS.JSONSerializer 将为 UserRoute extract() / code>和 extractMany() UsersRoute 。然而,有趣的是,大部分时间 extractMany()(用于获取所有用户的JSON返回)将在 extract()为单个用户及其侧面加载的属性。当发生这种情况时,侧面加载的属性确实会呈现给模板。然而,每一次 extract()将在 extractMany()之前(它异步地击败提取许多),侧面加载的属性将不会呈现。我认为这是因为如果首先出现 extract(),那么当$ code> extractMany()然后,该模型将被重置发生在所有的模型,当提取很多没有侧面的属性。

Therefore, when navigating to the route 'clients/3' the DS.JSONSerializer would do an extract() for the UserRoute and an extractMany() for the UsersRoute. However, interestingly enough, most of the time extractMany() (for getting a JSON return of all of the users) would occur before extract() for the single user and its sideloaded properties. When this happened the sideloaded properties would indeed render to the template. However, every once in a while extract() would come before extractMany() (it asynchronosly "beat" the extract many), the sideloaded properties would not render. I think this is because if the extract() occured first that model would then be reset when the extractMany() then occurred for all of the models, which when extracting many do not have sideloaded properties.

我通过执行以下操作来修复这个第一个问题:

I fixed this first problem by doing the following:

App.Router.map(function() {
  this.resource('users', function() {
    this.route('create');
  });

  this.resource('user', { path: 'user/:user_id' }, function() {
    this.route('edit');
    this.route('delete');
  });
});

这样可以防止在同一路由中提取两个模型,但是以下可能已经解决了这两个问题。

This prevented both models from being extracted in the same route, but the following might have solved both problems.

第二个问题当从 client / 3 导航到客户端然后再次返回到 client / 3 ,模型将像第一个问题一样重置,侧面加载的属性将被丢弃。

Second Problem: When navigating away from client/3 to clients and then back to client/3 again, the model would get reset just like the first problem—-sideloaded properties would get dropped.

解决此问题的方法是使用 UserRoute activate 钩子重新加载模型。

The way to fix this was to use the UserRoute's activate hook to reload the model.

App.UserRoute = Ember.Route.extend({
  activate: function() {
    this.modelFor('user').reload();
  }
});

这将强制模型在每次此路由激活时重新加载侧边属性,需要我们正在建设的这个特定的应用程序。

This will force the model to be reloaded with the sideloaded properties every time this route 'activates', which is needed of this particular app we're building anyway.

希望这有助于某人!

这篇关于Ember数据侧面加载的属性被丢弃在模型上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆