2014-07-18 20 views
0

我正在編寫一個小示例應用程序,它將在儀表盤和表中向用戶顯示一些信息。在上下文更改時刷新會話數據以外的所有數據

該應用程序正在使用ember-cli版本0.0.37編寫。和ember.js版本1.5.1。

我使用ember-simple-authember-simple-auth-oauth2進行身份驗證,使用自定義的認證和authroizer注入CLIENT_ID,client_secret和ACCESS_TOKEN到我充分認識到客戶端機密不應該是在前端應用在適當情況下(請求,但這僅僅是一個內部消費的示例應用程序,而不是我的問題的主題)。

自定義授權程序還向請求中注入o參數,其值是組織標識。 API返回數據使用access_token和o參數來返回與特定組織有關的數據。組織標識存儲在會話中。

所以,一旦我瀏覽了一個組織,我已經有了一個下拉組件,它允許我選擇另一個組織。目前,這會在ApplicationRoute上調用更新會話中的值的操作,並清除所有不屬於帳戶或組織的模型的存儲,因爲它們在應用程序級別使用。

代碼:

setSessionOrganisation: function(organisation_id) { 
    var self = this; 

    /** 
    * Check if its the same organisation 
    */ 
    if (this.get('session.organisation_id') == organisation_id) { 
     return; 
    } 

    /** 
    * Get the organisation for the id provided 
    */ 
    this.store.find('organisation', organisation_id).then(
     function(org) { 

      /** 
      * Update the session details 
      */ 
      self.set('session.organisation_id', org.get('id')); 
      self.set('session.organisation_name', org.get('name')); 

      /** 
      * Get all types 
      */ 
      var store = self.container.lookup('store:main'); 
      var types = []; 
      var typeMaps = store.typeMaps; 
      $.each(typeMaps, function(key) { 
       if (typeMaps.hasOwnProperty(key)) { 
        var type = typeMaps[key].type.typeKey; 
        if (type != 'account' && type != 'organisation'){ 
         types.push(typeMaps[key].type.typeKey); 
        } 
       } 
      }); 

      /** 
      * Clear data for types 
      */ 
      for (var i = 0; i < types.length; i++) { 
       store.unloadAll(types[i]); 
      }; 
     }); 
} 

我覺得上面的代碼是一個有點hackish,但我沒有找到另一種方式目前在店裏返回模型類型。

當這個動作被調用,並且數據被刷新後,我想刷新/重新加載當前路由。一些路線將是動態的,並使用對象ID作爲動態段。這些路線將需要重定向到他們的列表路線。其他路線在導航時會延遲加載數據。

所以我的問題是:

  • 有沒有更好的辦法來清除存儲數據比我上面已經做了什麼?
  • 如何觸發路線重新加載?
  • 如果在具有動態細分的路線中,我如何重定向到父路線?

作爲一個額外的問題,是否有任何危險卸載目前沒有顯示的意見/路線的所有數據?

謝謝!

+0

難道你只是重新加載頁面,以便商店被完全刷新(有效地刪除它可能持有的所有以前的數據爲不同的組織)? – marcoow

+0

@marcoow感謝您的建議,使用window.location.reload()或獲取應用程序和調用reset()會更好嗎?另外,我如何在ember-simple-auth會話中保留組織標識等值,因爲我設置的值不出現在localStorage中? –

+1

如果你想安全地清除數據,你需要重新加載頁面('window.location.reload()')。你可以簡單地用(this.get('session')。set('property','value')')來存儲數據。 – marcoow

回答

0

我來滿足我的標準,有很大的幫助,從@marcoow

按照有關問題的意見的決議中,我們討論重新加載整個應用程序刷新數據存儲緩存。雖然這種方法確實有效,但瀏覽器完全重新加載頁面時出現閃爍,我寧願只重新加載數據並讓應用程序自行排除。如果調用App.reset(),這個閃爍也會出現。

我探索了其他選項並找到了Ember.Route.refresh()方法。這似乎會導致嵌套路由在從應用程序路由中調用時重新加載模型。

在我的應用程序中,我已經將商店擴展到初始化程序,以便我可以調用一個函數,用於卸載商店中的所有記錄,用於商店中的每種類型的模型,還提供要排除的模型名稱列表:

app/initializers/custom-store.js

setSessionOrganisation: function(organisation_id) { 
    var _this = this; 

    /** 
    * Check if its the same organisation 
    */ 
    if (this.get('session.organisation_id') === organisation_id) { 
     return; 
    } 

    /** 
    * Get the organisation for the id provided 
    */ 
    this.store.find('organisation', organisation_id) 
     .then(
      function(org) { 

       /** 
       * Update the session details 
       */ 
       _this.get('session') 
        .set('organisation_id', org.get('id')); 
       _this.get('session') 
        .set('organisation_name', org.get('name')); 

       /** 
       * Clean the local data cache of all data pertaining to 
       * the old organisation 
       */ 
       _this.store.unloadAllExcept(['account', 'organisation']); 

       /** 
       * refresh the application route (and subsequently all 
       * child routes) 
       */ 
       _this.refresh(); 
      }); 
    } 

import DS from 'ember-data'; 

var CustomStore = DS.Store.extend({ 
    /** 
    * Extend the functionality in the store to unload all instances of each type 
    * of data except those passed 
    * 
    * @param {Array} exclusions List of model type names to exclude from data 
    *       unload 
    */ 
    unloadAllExcept: function(exclusions) { 

     var typeMaps = this.get('typeMaps'); 

     for (var type in typeMaps) { 
      if (typeMaps.hasOwnProperty(type)) { 

       var typeKey = (typeMaps[type].type && typeMaps[type].type.typeKey) ? typeMaps[type].type.typeKey : false; 

       if (typeKey && exclusions.indexOf(typeKey) === -1) { 
        this.unloadAll(typeKey); 
       } 
      } 
     } 
    } 
}); 


export 
default { 
    after: 'store', 
    name: 'custom-store', 

    initialize: function(container /*, app*/) { 
     container.unregister('store:main'); 
     container.register('store:main', CustomStore); 
    } 
}; 

這是從app/routes/application.jssetSessionOrganisationAction

作爲腳註,該解決方案不滿足我所問的第三個問題,但我意識到這個問題是與處理API響應有關的一個單獨問題,必須在模型鉤子期間處理。

相關問題