2013-07-03 4 views
4

새로운 비동기 라우터에서 비동기 적으로 추가 코드를로드 할 수 있습니다.Ember 비동기 라우터 : 여분의 경로로 비동기 코드로드

App.ArticlesRoute = Ember.Route.extend({ 
    beforeModel: function() { 
    if (!App.Article) { 
     return $.getScript('/articles_code.js'); 
    } 
    } 
}); 

그러나 비동기 코드에 추가 경로가 포함되어 있으면 어떻게 될까요? Ember가 생성했습니다. 하지만 Ember가 생성하고 등록한 코드가 아닌 비동기 코드의 코드가 필요합니다.

나는 다음과 같은 솔루션을 함께했다 (그러나 나는 그것으로 만족하지 않다) :

App.ArticlesRoute = Ember.Route.extend({ 
    beforeModel: function(transition) { 
    if (!App.Article) { 
     var self = this; 
     return $.getScript('/articles_code.js') 
     .then(function() { 
      return self.reRegisterRoutes(transition, "App", "Article"); //register the routes (with naming convention App.Article__________Route) from the loaded modules (dummies have been generated by Ember) and retry the transaction 
     }); 
    } 
    }, 

    reRegisterRoutes: function (transition, namespaceName, nameStartsWith) { 
    this.container.lookup('route:loading').teardownTopLevelView(); //teardown loading view 
    var self = this; 
    for (var prop in Ember.Namespace.byName(namespaceName).getOwnProperties()) { //[getOwnProperties](https://gist.github.com/adjohu/1817543) 
     if (new RegExp(nameStartsWith + '.+Route', 'i').test(prop)) { 
     this.reRegisterRoute(prop.replace(/Route$/, "").camelize()); 
     } 
    } 
    this.router.router.currentParams = transition.params; //set the parameters of the transition to the currentParams of the router 
    return transition.retry(); //retry transaction 
    }, 

    reRegisterRoute: function (routeName) { 
    var route = this.container.cache.dict['route:' + routeName]; 
    if (route != undefined) { 
     route.destroy(); //destroy generated route 
     delete this.container.cache.dict['route:' + routeName]; //GC 
     route = this.container.lookup('route:' + routeName); //register the async loaded route 
     route.routeName = routeName; //set the routeName 
    } 
    } 
}); 

어떤 제안?

EEPD.RouteMapper.map(function() { 
    this.route('home', { path: '/' }); 
    this.route('logout'); 
    this.route('login'); 

    this.resource('ccpr', function() { //ALL NEEDED FOR THE MENU TO WORK 
    this.resource('ccprPatients', { path: '/' }, function() { 
     this.route('search'); 
    }); 
    this.resource('ccprCardioArticles', { path: "/cardioarticles" }); 
    this.resource('ccprCardiologists', { path: "/cardiologists" }); 
    this.resource('ccprInfoSession', { path: "/infosession" }, function() { 
     this.route('index'); 
    }); 
    this.resource('ccprPatientPresence', { path: "/patientpresence" }, function() { 
    }); 
    this.resource('ccprPresenceOverview', { path: "/presenceoverview" }); 
    this.resource('ccprNextNutritionalAdvices', { path: "/nextnutritionaladvices" }); 
    this.resource('ccprParameter', { path: "/parameter" }); 
    this.resource('ccprListStaff', { path: "/liststaff" }); 
    }); 
}); 

는 다음 구분로드 파일

EEPD.RouteMapper.map(function() { 
    this.resource('ccpr', function() { 
    this.resource('ccprPatients', { path: '/' }, function() { 
     this.route('search'); 
    }); 
    this.resource('ccprPatient', { path: '/:ccpr_patient_id' }, function() { 
     this.resource('ccprPracticeSessions', { path: '/practicesessions' }, function() { 
     }); 
     this.resource('ccprPracticeSession', { path: '/practicesessions/:ccpr_practiceSession_id' }, function() { 
      this.route('info'); 
      this.route('anamnese'); 
      this.route('medication'); 
      this.route('trainingModel', { path: '/trainingmodel' }); 
      this.route('socialEvaluation', { path: '/socialevaluation' }); 
      this.route('medicalFollowUp', { path: '/medicalfollowup' }); 
      this.resource('ccprPracticeSessionPsychologicalEvaluation', { path: '/psychologicalevaluation' }, function() { 
       this.resource('ccprPracticeSessionPsychologicalEvaluationMnhds', { path: '/mnhd' }); 
       this.resource('ccprPracticeSessionPsychologicalEvaluationMnhd', { path: '/mnhd/:ebed_questionaire_id' }); 
       this.resource('ccprPracticeSessionPsychologicalEvaluationHadss', { path: '/hads' }); 
       this.resource('ccprPracticeSessionPsychologicalEvaluationHads', { path: '/hads/:ebed_questionaire_id' }); 
       this.resource('ccprPracticeSessionPsychologicalEvaluationDs14s', { path: '/ds14' }); 
       this.resource('ccprPracticeSessionPsychologicalEvaluationDs14', { path: '/ds14/:ebed_questionaire_id' }); 
      }); 
      this.resource('ccprPracticeSessionNutritionalAdvice', { path: '/nutritionaladvice' }, function() { 
       this.resource('ccprPracticeSessionNutritionalAdviceBmi', { path: '/bmi/:ebed_nutritionBmi_id' }); 
       this.resource('ccprPracticeSessionNutritionalAdviceDietContact', { path: '/dietcontact/:ebed_dietContact_id' }); 
      }); 
      this.route('decision', { path: '/decision' }); 
      this.resource('ccprPracticeSessionApprovals', { path: '/approvals' }, function() { 
       this.resource('ccprPracticeSessionApproval', { path: '/:erevalidatie_approval_id' }); 
      }); 
     }); 
    }); 
    this.resource('ccprCardioArticles', { path: "/cardioarticles" }); 
    this.resource('ccprCardiologists', { path: "/cardiologists" }); 
    this.resource('ccprInfoSession', { path: "/infosession" }, function() { 
     this.route('addPatient'); 
    }); 
    this.resource('ccprPatientPresence', { path: "/patientpresence" }, function() { 
     this.route('index'); 
     this.route('addPatient'); 
    }); 
    this.resource('ccprPresenceOverview', { path: "/presenceoverview" }); 
    this.resource('ccprNextNutritionalAdvices', { path: "/nextnutritionaladvices" }); 
    this.resource('ccprParameter', { path: "/parameter" }); 
    this.resource('ccprListStaff', { path: "/liststaff" }); 
    }); 
}, true); 

에서 그렇지 않은 :

UPDATE DARSHAN-sawardekar @

나는 솔루션 끝이와 결국 시도
, 나는 그것을 내 솔루션과 결합 할 때만 부분적으로 작동합니다. 어설 같은 내가 가지고 오류 실패 : ccprPracticeSession.info.index

답변

1

당신은 다른 개체로 모든 경로지도 저장할 수를라는 이름의 경로가 없습니다, 다음 Router.map 전화 : 경로의 ccprPracticeSession.info이 및 catch되지 않은 오류를 찾을 수 없습니다 지도를 반복합니다. 서브 모듈로드 코드가 저장되어있는지도에서 경로의 전체 세트를 다시 것 App.RouteMapper.map를 호출

App.RouteMapper = Ember.Object.extend({ 
}); 

App.RouteMapper.reopenClass({ 
    maps: null, 

    map: function(callback, reassign) { 
    if (!this.maps) this.maps = new Array(); 

    this.maps.push(callback); 
    if (reassign) { 
     this.assign(); 
    } 
    }, 

    assign: function() { 
    var self = this; 
    App.Router.map(function() { 
     var routerScope = this; 
     self.maps.forEach(function(map) { 
     map.call(routerScope); 
     }); 
    }); 
    } 
}); 

App.RouteMapper.map(function() { 
    this.route('lorem'); 
    this.route('ipsum'); 
}); 

뭔가처럼. mapsRouter.map으로 전달할 때와 동일한 콜백이므로 api는 변경되지 않습니다.

+0

당신의 솔루션은 라우터 맵을 분리하는 데 유용합니다. 그러나 그것은 내가 의미하는 바가 정확히 아닙니다. 메뉴가있는 경우 메뉴의 모든 라우터 맵을 시작시로드해야하지만이 라우터 맵 뒤에 비동기로드를 원합니다 (Ember는 더미 테이블을 생성하여 컨테이너에 등록합니다). – pjlammertyn

+0

'RouteMapper'는지도를 분할하여 작동하지만지도 콜백을 유지합니다. 그래서 당신은'RouteMapper.map'을 외부 서브 모듈의 내부 코드에서 N 번 호출 할 수 있고, 2 번째 재 할당 파라미터를 사용하여 경로를 다시 빌드 할 수 있습니다. 실제 'RouteMapper.map' 호출은 메인 앱 내부가 아니더라도 어디에서나 호출 할 수 있습니다. 그것이 바로 초기 경로가있는 곳입니다. [jsbin] (http://jsbin.com/oqogiw/1/edit)을 참조하십시오. 추가 경로는 여기의 버튼 클릭에 있지만 서브 모듈 내부에있을 수도 있습니다. –