2013-02-12 34 views
6

在Ember中存儲全局變量的適當方法是什麼?例如,我在Ember中有一個用戶模型,但我總是想知道該模型的哪個特定實例(包括其ID,名稱,電子郵件等)與當前登錄的用戶相對應。Ember中的全局變量

我應該以某種方式將它存儲在Ember中嗎?或者,將一個JS變量附加到window對象(例如window.currentUserId = 1;)並使用它更好?

+0

至少使用'User'對象作爲'window.user =。 ..'。 – Reactormonk 2013-02-12 23:18:44

+1

您可以將該狀態存儲在您的應用程序實例中。 App = Ember.Application.create({currentUser:null})。當你準備好時,用App.set(「user」,user)設置用戶。 – mavilein 2013-02-12 23:22:22

+0

我在我的應用程序中存儲了一些中央狀態,它工作正常。它有點臭,但對我來說似乎是可行的,如果不過度使用這種方法。 – mavilein 2013-02-12 23:23:34

回答

8

免責聲明:以下所示的所有技術都是我在過去幾個月中對EmberJS的經驗,以及與同事的一些討論。有任何不正確的地方,請提高你的聲音。我們都在學習階段(和ember.js文件吸到maxxx,所以要善良)

當前有兩個開發人員在我的工作場所使用Ember.js。我們得出的結論是,在全局ApplicationController中存儲全局變量比存儲在應用程序名稱空間中好得多。這是因爲如果存儲在應用程序名稱空間中,則此值的檢索可能非常混亂。而且這也適用於閉包,因此可以使全局名稱空間變得乾淨(並且相對免費)。你不希望你的用戶做App.set弄亂變量的權利?

這是基於1.0.0之前的4。

考慮你有這樣的currentUserId全局變量

  1. 命名空間中的存儲。 jsFiddle demo

    (function() { 
    
        function r() { 
         return Math.round(Math.random()*999); 
        } 
    
        var MyApp = Ember.Application.create({ 
         currentUserId: null, 
         ready: function() { 
          //demo purpose. 
          this.set('currentUserId', r()); 
         }, 
         rootElement: '#demo' 
        }); 
        MyApp.ApplicationView = Ember.View.extend({ 
         templateName: 'application-view', 
    
         //Direct child view 
         innerView: Ember.View.extend({ 
          templateName: 'inner-view', 
          setValue: function() { 
           this.set('controller.namespace.currentUserId', r()); 
          } 
         }), 
    
         //Direct child view, but with a controller attached 
         innerViewWithController: Ember.View.extend({ 
          controller: Ember.Controller.create(), 
          templateName: 'inner-view-with-controller', 
          setValue: function() { 
           this.set('parentView.controller.namespace.currentUserId', r()); 
          } 
         }), 
         getValue: function() { 
          alert(this.get('controller.namespace.currentUserId')); 
         }, 
         setValue: function() { 
          this.set('controller.namespace.currentUserId', r()); 
         } 
        }); 
    })(); 
    
  2. VS在全球ApplicationControllerjsFiddle demo

    (function() { 
    
        function r() { 
         return Math.round(Math.random()*999); 
        } 
    
        var MyApp = Ember.Application.create({ 
         ApplicationController: Ember.Controller.extend({ 
          currentUserId: null, 
          init: function() { 
           //demo purpose 
           this.set('currentUserId', r()); 
          } 
         }), 
         rootElement: '#demo' 
        }); 
        MyApp.ApplicationView = Ember.View.extend({ 
         templateName: 'application-view', 
    
         //Direct child view 
         innerView: Ember.View.extend({ 
          templateName: 'inner-view', 
          setValue: function() { 
           this.set('controller.currentUserId', r()); 
          } 
         }), 
    
         //Direct child view, but with a controller attached 
         innerViewWithController: Ember.View.extend({ 
          controller: Ember.Controller.create(), 
          templateName: 'inner-view-with-controller', 
          setValue: function() { 
           this.set('parentView.controller.currentUserId', r()); 
          } 
         }), 
         getValue: function() { 
          alert(this.get('controller.currentUserId')); 
         }, 
         setValue: function() { 
          this.set('controller.currentUserId', r()); 
         } 
        }); 
    })(); 
    

注意存放的是:

  1. 如果選擇在命名空間來存儲,則需要通過訪問它始終是根控制器,所以它實際上與存儲相同applicationController帶有額外的namespace關鍵字。

  2. 如果選擇將其存儲在根applicationController,不管什麼從applicationView下降的觀點,你可以很容易地{{variableName}}訪問變量在模板中沒有任何點遍歷。默認情況下,Ember.Js通過控制器查找變量。

  3. 在最糟糕的情況下,如果你的內部視圖需要自己的控制器,通過根控制器(或命名空間)訪問全局變量稍微痛苦一點,因爲控制器沒有鏈接,你需要遍歷你的視圖直到你看到根控制器。在Ember.JS中,默認情況下,所有視圖都將設置一個控制器,默認情況下爲父級控制器。這意味着如果你從不指定任何控制器,所有後代視圖實際上都鏈接到根控制器。爲了克服這個問題,你可以在控制器中做變量綁定來輕鬆解決遍歷醜。

我不建議你把這樣一個重要的變量,在全球window對象,因爲它會被用戶方便地進行修改(並提出任何潛在的問題)。把它在灰燼應用程序命名空間中的全局命名空間減少了潛在的問題,但不是如果你把它的全球

我的2美分;)