6

リソースがすでにストアにある場合、Ember Data にサーバー イーブンからリソースをロードさせる良い方法はありますか?

モデルが最初にページを表示しようとしたときに1回だけ読み込まれるという単純なユーザーアクションstore.find('users',id)を表示します。2回目にモデルがストアから読み込まれますが、これは私が知っている通常のemberデータの動作です。ただし、毎回ロードする必要があります。

編集:私が見つけた唯一の方法はこれを行うことです:

@store.find('user',{id: params.user_id}).then (users)->
  users.get('firstObject')

ただし、インデックスアクションに「偽の」表示アクションを実装する必要があります...

4

6 に答える 6

5

これだと思います... http://emberjs.com/api/data/classes/DS.Model.html#method_reload

model.reload()

幸運を

于 2013-11-11T14:52:19.953 に答える
3

さらに、getByIdwhich を呼び出して、存在するそのレコードのインスタンスまたは null を返し、呼び出しunloadRecordてキャッシュから削除することができます。私は Edu の反応も気に入っていますが、レコードがどこかに存在することを心配する必要はありません。たぶん、ユーザーへの参照が更新された参照をそのようgetByIdに使用するでしょう。reload(間違っている場合は、私の誤ったコーヒースクリプトを許してください)。

user = @store.find('user', params.user_id)
if (user) 
  @store.unloadRecord(user)
于 2013-11-11T16:50:49.130 に答える
2

machtyのおかげで、プレスを熱く:

Route.refresh() と呼ばれる今週末にベータ版になるクエリ パラメータ機能の一部として追加される新しいメソッドがあります...

/**
Refresh the model on this route and any child routes, firing the
`beforeModel`, `model`, and `afterModel` hooks in a similar fashion
to how routes are entered when transitioning in from other route.
The current route params (e.g. `article_id`) will be passed in
to the respective model hooks, and if a different model is returned,
`setupController` and associated route hooks will re-fire as well.

An example usage of this method is re-querying the server for the
latest information using the same parameters as when the route
was first entered.

Note that this will cause `model` hooks to fire even on routes
that were provided a model object when the route was initially
entered.

@method refresh
@return {Transition} the transition object associated with this
  attempted transition
@since 1.4.0
*/
于 2014-06-02T22:23:11.403 に答える
1

これは、promise と Edu で言及されている reload メソッドを使用して、setupController フックで実行できます。

  setupController: ->
    @store.find('myModel', 1).then (myModel) ->
      myModel.reload()
于 2014-06-03T09:04:13.703 に答える
0

特定のアクションの後に表示するレコードが変更されることが確実な場合はthis.refresh()、ルートでメソッドを呼び出すことができます。例えば:

ProductsRoute = Ember.Route.extend
  model: ->
    @store.find 'product',
      activated: true

  actions:
    accept: (product) ->
      if not product.get('activated')
        product.set 'activated', true
        product.save()
          .catch (err) ->
            console.log err
            product.rollback()
          .then =>
            @refresh()

    ignore: (product) ->
      if not product.get('ignored')
        product.set 'ignored', true
        product.save()
          .catch (err) ->
            console.log err
            product.rollback()
          .then =>
            @refresh()

アクションが子ルートから呼び出された場合 (製品/提案など)、親ルートと子ルートのモデルがリロードされます。

于 2015-01-17T16:09:29.347 に答える
0

あなたが探しているのはDS.Store#fetchByIdだと思います

于 2015-05-21T12:33:39.253 に答える