简体   繁体   English

灰烬嵌套的路线和承诺

[英]Ember nested routes and promises

When accessing an ember URL directly with nested routes, the app does not wait for parent promises to resolve before loading nested resources. 当直接使用嵌套路由访问余烬URL时,应用程序不会等待父承诺解析后再加载嵌套资源。

Here's an example code: 这是一个示例代码:

var App = Ember.Application.create();

App.Router.map(function () {
    this.resource('index', {path: "/"}, function () {
        this.resource('project', {path: "/project/:id"}, function () {
            this.resource('file', {path: "/file/:encodedPath"});
        });
    });
});

App.IndexRoute = Ember.Route.extend({
    model: function () {
        return Ember.$.getJSON('/list-projects');
    }
});

App.ProjectRoute = Ember.Route.extend({
    setupController: function (controller, model) {
        return Ember.$.getJSON('/load-project?id=' + model.id).then(
                function (data) {
                    controller.set('model', data);
                });
    }
});

App.FileRoute = Ember.Route.extend({
    setupController: function (controller, model) {
        var project = this.modelFor('project');
        var url = '/load-file?project=' + project.id + '&path=' + model.encodedPath;

        return Ember.$.getJSON(url).then(
            function (data) {
                controller.set('model', data);
            });

    }
});

Is it possible to wait for the project data to resolve before loading file data? 加载文件数据之前是否可以等待项目数据解析?

The problem only occurs when loading the URL directly in the browser: http://mywebsite.com/#/project/1234/file/test.txt 仅在直接在浏览器中加载URL时,才会出现此问题: http : //mywebsite.com/#/project/1234/file/test.txt

Thanks. 谢谢。

As per my comments to your question, this should do the trick. 根据我对您的问题的评论,这应该可以解决问题。 Note the model hook can return a Promise whereas setupController shouldn't return anything. 请注意, model挂钩可以返回PromisesetupController不应该返回任何东西。

Here is a JSBin that tries correspond with your project - obviously the data is different: http://emberjs.jsbin.com/letalehoje/1/edit?html,js,console,output 这是一个尝试与您的项目相对应的JSBin-显然数据是不同的: http ://emberjs.jsbin.com/letalehoje/1/edit?html,js,console,output

Make sure you view it externally as well: http://emberjs.jsbin.com/letalehoje/1/#/project/1/file/text1-1.txt 确保您也从外部查看它: http : //emberjs.jsbin.com/letalehoje/1/#/project/1/file/text1-1.txt

App.ProjectRoute = Ember.Route.extend({
    model: function(params) {
        return Ember.$.getJSON('/load-project?id=' + params.id);
    }
});

App.FileRoute = Ember.Route.extend({
    model: function(params) {
        var projectPromise = this.modelFor('project');

        return Ember.RSVP.resolve(projectPromise).then(function(project) {
            // this is the promise that should be returned for the FileRoute model
            return Ember.$.getJSON('/load-file?project=' + project.id + '&path=' + params.encodedPath);
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM