0

2 つの単純なモデル (ember-model) を持つ単純な EmberJS アプリケーションがあります。アカウントとアイテム。アカウントには多くのアイテムがあります。

したがって、アプリケーション内のリンクを使用して #/accounts/1/items に移動すると、完全に正常に動作します。ただし、 #/accounts/1/items を直接リロードすると、エラーが発生します。

Assertion failed: The value that #each loops over must be an Array. You passed <App.Account:ember335> (wrapped in (generated items controller)) ember.js?body=1:382
Uncaught TypeError: Object [object Object] has no method 'addArrayObserver' ember.js?body=1:19476
Assertion failed: Emptying a view in the inBuffer state is not allowed and should not happen under normal circumstances. Most likely there is a bug in your application. This may be due to excessive property change notifications. ember.js?body=1:382

これは私のアプリがどのように見えるかです:

App.Router.map ()->
  @resource 'accounts', ->
    @resource 'account', path: ':account_id', ->
      @resource 'items'

App.AccountRoute = Ember.Route.extend
  model: (params) ->
    App.Account.find(params.account_id)

App.ItemsRoute = Ember.Route.extend
  model: ->
    @.modelFor('account').get('items')

App.Account = Ember.Model.extend
  name: Ember.attr('string')
  item_ids: Ember.attr(),
  items: (->
    App.Items.find(@.get('comment_ids'))
  ).property('comment_ids')

App.Item = Ember.Model.extend
  name: Ember.attr('string')

コントローラは標準 (空) です。

JS コンソールでは、次のような呼び出しは正常に機能し、エラーがスローされた (何もレンダリングされない) 後でも正しい結果を返します。

 App.Account.find(1).get('items')

なぜこれが起こっているのかわかりません。コードは非常に単純なので、手がかりがないのは本当に面倒です。誰にもアイデアがありますか?

4

1 に答える 1

0

私は残り火データの専門家ではありませんが、約束を返しているようです。したがって、次のことを試してください。

App.ItemsRoute = Ember.Route.extend({
  model : function(){
    var accountPromise = App.Account.find(1);
    var itemsPromise = Ember.Deferred.create();
    accountPromise.then(function(account){
      itemsPromise.resolve(account.get("items"));
    });
    return itemsPromise;
  }
});

なぜそのようにしなければならないのですか?

  1. App.Account.find(1);非同期呼び出しを実行するため、promise を返します。
  2. そのため、アイテムをすぐに返却することはできません。accountPromise が満たされるまで待つ必要があります。
  3. itemspromiseaccountPromise が満たされると満たされる新しい promise ( ) を返します。
  4. Promise を返すため、Ember は Promise が満たされるのを待ち、その結果を Controller のモデルとして使用します。

PS:実際、これは少し複雑に思えます。これでうまくいくと思いますが、もっとエレガントな解決策があるかもしれません。

于 2013-08-28T13:04:39.747 に答える