2013-03-27 80 views
4

我現在處理用戶登錄/註冊時沒有餘燼,但我想處理用戶配置文件(編輯配置文件)。擁有一個能夠處理當前用戶的單體模型的最佳行動是什麼?我定義了一個簡單的用戶爲:已登錄用戶的單身模型

App.User = DS.Model.extend({ 
    email: DS.attr('string'), 
    name: DS.attr('string'), 
}); 

我可以填充它在App.ready但我不太清楚如何與比App.User.find(id)任何其他做 - 我不知道ID。我的服務器可以從當前會話返回當前登錄的用戶,但在這種情況下如何使用它? 還是我以錯誤的方式處理這個問題?

感謝

回答

4

Discourse嵌入當前用戶的信息到基本HTML頁面,並預先載入到應用程序中的路線User模型:

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/routes/application_route.js

User模型,然後有足夠的信息來訪問用戶的個人資料/等:

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/models/user.js

會議的具體項目(如退出)訪問獨立/session/網址:如果您使用的會話

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse.js#L169

,並要避免嵌入在你的基地HTML頁面的當前用戶的信息(如您正在靜態提供內容等),您還可以手動訪問會話特定的URL(例如/session/),該URL會根據當前會話返回當前登錄的用戶信息。

4

我基本上按照你所描述的去做。我使用下面的代碼調用後端來獲取當前登錄的用戶並將其設置在Ember應用程序中。

var App = Ember.Application.create({ 
    LOG_TRANSITIONS: true, 
}); 

App.deferReadiness(); 

$.ajax({ 
    url: 'http://localhost:3000/login', 
    dataType: 'json', 
    success: function (resp) { 
     console.log(resp.user); 

     App.store.load(App.User, resp.user._id, resp.user); 
     App.store.load(App.Company, resp.company._id, resp.company); 

     var user = App.User.find(resp.user._id); 
     var company = App.Company.find(resp.company._id); 

     App.CurrentUserController.reopen({ 
      content: user 
     }); 

     App.CurrentCompanyController.reopen({ 
      content: company 
     }); 

     App.advanceReadiness(); 
    }, 
    error: function (resp) { 
     console.log('failed: ' + resp); 
     App.advanceReadiness(); 
    } 
}); 

App.initialize(); 
+1

什麼是當前的方法在新的Ember中做同樣的事情?因爲,它是說load()沒有被定義。 – 2014-03-16 12:23:55