2013-07-03 3 views
4

Dans le nouveau routeur asynchrone, vous pouvez charger async du code supplémentaire.Routeur asynchrone Ember: code de chargement asynchrone avec routes supplémentaires

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

mais que se passe-t-il si le code asynchrone contient des routes supplémentaires? Ember les aura générés. Mais j'ai besoin de ceux du code asynchrone, pas de ceux générés et enregistrés par Ember.

je suis venu avec la solution suivante (mais je ne suis pas content):

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 
    } 
    } 
}); 

des suggestions?

MISE À JOUR @ darshan-sawardekar

J'ai essayé la fin de votre solution a fini avec ceci:

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" }); 
    }); 
}); 

puis dans le fichier chargé seperated

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); 

Il n'a pas d' travailler, seulement si je le combine avec ma solution, il fonctionne partiellement. erreurs comme je m'y suis Assertion a échoué: Le ccprPracticeSession.info de la route n'a pas été trouvé et Uncaught Erreur: Il n'y a pas d'itinéraire nommé ccprPracticeSession.info.index

Répondre

1

Vous pouvez stocker toutes les cartes de route dans un autre objet, puis appelez Router.map , itérant à travers les cartes. Quelque chose comme,

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'); 
}); 

Le code chargé appelle sous-module App.RouteMapper.map qui reconstruire l'ensemble des routes des cartes stockées. Les maps sont les mêmes rappels que vous passeriez à Router.map pour que l'API ne change pas.

+0

Votre solution est utile pour diviser la carte du routeur. Mais ce n'est pas exactement ce que je veux dire. Si vous avez un menu, toutes les cartes de routeur dans le menu doivent être chargées au démarrage, mais je veux charger async les routes derrière ces cartes de routeur (Ember aura généré des cartes fictives et les aura enregistrées dans son conteneur). – pjlammertyn

+0

Bien que le 'RouteMapper' fonctionne en séparant les cartes, il préserve les callbacks de la carte. Vous pouvez donc appeler le N RouteMapper.map depuis le code interne dans un sous-module externe et avec le second paramètre de réaffectation, reconstruire également les routes. L'appel 'RouteMapper.map' réel peut être n'importe où, pas nécessairement dans l'application principale. C'est juste là que vous aurez les routes initiales. Voir [jsbin] (http://jsbin.com/oqogiw/1/edit). Les routes d'ajout sont sur les clics de bouton ici mais pourraient également être à l'intérieur des sous-modules. –