๋จ์ ํ ์คํธ๋ฅผ ์์ฑํ๋ ค๊ณ ํ๋ ์ธ์ฆ ์๋น์ค๊ฐ ์์ต๋๋ค.
Auth ์๋น์ค์ ์ ์ฅ๋ ์ด์ ์ ํ์ด ์์ผ๋ฉด ํน์ ๊ฒฝ๋ก๋ก ๋ฆฌ๋๋ ์ ๋ฉ๋๋ค.
์ฑ์ ์์๋๋ก ์๋ํ์ง๋ง ์ด ์๋น์ค์ ๋ํ ํ ์คํธ์์ ๋ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
Uncaught TypeError: Cannot read property 'hasRoute' of undefined
๋๋ฒ๊น /ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ๋ฌด์จ ์ผ์ด์ผ?
๋ด ์ธ์ฆ ์๋น์ค๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
import Ember from 'ember';
import config from '../config/environment';
export default Ember.Service.extend({
routing: Ember.inject.service('-routing'),
loginCapable: true,
localStorageKey: config.localStorageKeys.user,
checkAuth(priorTransition){
this.set('priorTransition', priorTransition);
return this.get('isLoggedIn');
},
isLoggedIn: Ember.computed('user', function(){
return !!this.get('user');
}),
init(){
this.set('user', JSON.parse(window.localStorage.getItem( this.get("localStorageKey") )) );
},
login(adID){
return Ember.$.ajax(
config.apiHost + config.apiVersion + "/getLdapInfo",{
type: 'GET',
data: "adID="+adID
}).then(json => {
this.set('user', json);
window.localStorage.setItem( this.get("localStorageKey") , JSON.stringify(json));
var priorTransition = this.get('priorTransition');
if( priorTransition )
{
priorTransition.retry();
this.set('priorTransition', null);
}else{
this.get('routing').transitionTo('index');
}
}, xhr => {
return xhr.responseJSON ? xhr.responseJSON.message : "Login failed";
});
}
});
๊ทธ๋ฆฌ๊ณ ๋ด ๋ผ์ฐํฐ:
import Ember from 'ember';
import config from './config/environment';
var Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function() {
this.route('authenticated', { path: '/' }, function(){
this.route('index', { resetNamespace: true });
});
this.route('login');
});
export default Router;
ํด๋น ์๋น์ค์ ๋ํ ํ ์คํธ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
import { moduleFor, test } from 'ember-qunit';
import Ember from 'ember';
import startApp from 'recruiter-admin/tests/helpers/start-app';
import Pretender from 'pretender';
var server,
application;
var user = [{
"adID":"uniqueID",
"first_name":"First Name",
"last_name":"Last Name",
"employee_type":"Regular",
"employee_id":"12345",
"manager":"CN=managerID,OU=Employees,OU=company Users,DC=company,DC=com",
"success":true
}];
function jsonResponse(json, status = 200) {
return [status, {"Content-Type": "application/json"}, JSON.stringify(json)];
}
moduleFor('service:auth', 'Unit | Service | auth', {
unit:true,
needs:['router:main'],
beforeEach(){
application = startApp();
server = new Pretender(function(){
this.get('/getLdapInfo', function(req){
return jsonResponse(user.findBy('adID',req.queryParams.adID));
});
});
},
afterEach(){
Ember.run(application, 'destroy');
if( server ){
server.shutdown();
}
}
});
// Replace this with your real tests.
test('it exists', function(assert) {
var service = this.subject();
assert.ok(service);
});
test('isLoggedIn Testing', function(assert){
var service = this.subject();
assert.ok(service.get('isLoggedIn') === false, 'Not logged in');
Ember.run(()=>{
service.set('user', true);
});
assert.ok(service.get('isLoggedIn') === true, 'IS logged in');
});
test('can login', function(assert){
assert.expect(0);
var service = this.subject();
Ember.run(()=>{
service.login('uniqueID');
});
andThen(()=>{
assert.ok(service.get('user'));
});
});
Ember 2.0.0-beta.5 ์ฌ์ฉ
๋น๊ณต๊ฐ -routing
์๋น์ค๋ฅผ ์ฌ์ฉ ์ค์ด๋ฉฐ router
์ธ์คํด์ค๊ฐ ์ด๊ธฐํ ๋ฐ ์ค์ ๋์ด์ผ ํฉ๋๋ค. ์ด ํ
์คํธ๋ ๊ทธ๋ ๊ฒ ํ์ง ์์ผ๋ฏ๋ก ์ด๋ฅผ ์กฐ๋กฑํ๊ฑฐ๋ ๋ผ์ฐํฐ๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค.
์ด ์ค๋ฅ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด https://github.com/emberjs/ember.js/blob/master/packages/ember-routing/lib/services/routing.js#L37 ์ ์ฝ๊ฐ ์กฐ์ ํ๋ ๊ฒ์ ๋ฐ๋ํ์ง ์์ต๋๋ค(๊ถ๊ทน์ ์ผ๋ก ๋ฐํ๋์ด์ผ ํฉ๋๋ค. get(this, 'router')
๊ฐ false์ธ ๊ฒฝ์ฐ false) PR์ ์ ์ถํ๊ณ ์ถ์ง๋ง ์ง์๋์ง ์๋ ๋ฐฉ์์ผ๋ก ๋น๊ณต๊ฐ API๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋ฒ๊ทธ๊ฐ ์๋๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ๋ง๋ฌด๋ฆฌํ์ง๋ง ๊ธฐ๊บผ์ด PR์ ๊ฒํ ํ๊ฒ ์ต๋๋ค.
๊ฒ์์ ๋ฆ์์ง๋ง ๊ทธ๋ฐ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋ด๊ฐ ํ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ฃผ๊ณ ์ถ์ต๋๋ค.
๋ฉ์๋๋ฅผ ์ฌ์ ์ํ๋ฉด ๋ฉ๋๋ค.
const service = this.subject({
routing: {
transitionTo() {
return true;
}
}
})
๋ค๋ฅธ ๊ฒฝ๋ก ์์ฒด๋ฅผ ํ
์คํธํ๊ธฐ ์ํ ํ
์คํธ๋ฅผ ์์ฑํ์ผ๋ฉฐ ember/service/routing.js
ํ
์คํธ๋ฅผ ์ํด ember ํ์ ์ ๋ขฐํ๊ธฐ ๋๋ฌธ์ ์ด ์์
์ ์ํํ๊ณ ์์ต๋๋ค.
this.owner.unregister('service:router');
this.owner.register('service:router', mocks.routerService());
ํ ์คํธ ์ผ์ด์ค๋ฅผ ์คํจํ์ง ์๊ณ ์คํฐ๋นํ๋ ํ ๊ฐ์ง ๋ฐฉ๋ฒ
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋น๊ณต๊ฐ
-routing
์๋น์ค๋ฅผ ์ฌ์ฉ ์ค์ด๋ฉฐrouter
์ธ์คํด์ค๊ฐ ์ด๊ธฐํ ๋ฐ ์ค์ ๋์ด์ผ ํฉ๋๋ค. ์ด ํ ์คํธ๋ ๊ทธ๋ ๊ฒ ํ์ง ์์ผ๋ฏ๋ก ์ด๋ฅผ ์กฐ๋กฑํ๊ฑฐ๋ ๋ผ์ฐํฐ๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค.์ด ์ค๋ฅ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด https://github.com/emberjs/ember.js/blob/master/packages/ember-routing/lib/services/routing.js#L37 ์ ์ฝ๊ฐ ์กฐ์ ํ๋ ๊ฒ์ ๋ฐ๋ํ์ง ์์ต๋๋ค(๊ถ๊ทน์ ์ผ๋ก ๋ฐํ๋์ด์ผ ํฉ๋๋ค.
get(this, 'router')
๊ฐ false์ธ ๊ฒฝ์ฐ false) PR์ ์ ์ถํ๊ณ ์ถ์ง๋ง ์ง์๋์ง ์๋ ๋ฐฉ์์ผ๋ก ๋น๊ณต๊ฐ API๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋ฒ๊ทธ๊ฐ ์๋๋๋ค.