首页 文章

强制ember数据store.find从服务器加载

提问于
浏览
6

是否有一种很好的方法可以强制Ember Data从服务器eaven加载资源,如果它已经存储了?

我有一个简单的显示用户动作 store.find('users',id) 模型只加载一次,第一次尝试显示页面我第二次去我的模型从商店加载,这是我知道的正常的余烬数据行为 . 但是我每次都需要加载它 .

编辑:我找到的唯一方法是这样做:

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

但它迫使我对我的索引动作实施“虚假”的表演行动......

6 回答

  • 1

    我想这个... http://emberjs.com/api/data/classes/DS.Model.html#method_reload

    model.reload()
    

    祝好运

  • 0

    此外,您可以调用 getById ,它将返回该记录的任何实例,或者为null,然后调用 unloadRecord 将其从缓存中删除 . 我喜欢Edu 's response as well though, then I wouldn' t不得不担心其他地方存在的记录 . 也许我会使用 getById 然后 reload ,这样任何引用用户的引用都会更新 . (原谅我错误的咖啡,如果错的话) .

    user = @store.find('user', params.user_id)
    if (user) 
      @store.unloadRecord(user)
    
  • 5

    热卖presses,感谢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
    */
    
  • 0

    您可以在setupController挂钩中使用promise和Edu提到的重载方法执行此操作 .

    setupController: ->
        @store.find('myModel', 1).then (myModel) ->
          myModel.reload()
    
  • 3

    如果您确定要显示的记录在某个操作后会发生变化,那么您可以在路径中调用 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()
    

    如果从子路线呼叫动作 - 例如产品/建议 - 将为父路线和儿童路线重新加载模型 .

  • 2

    我认为你要找的是DS.Store#fetchById

相关问题