2013-06-29 29 views
2

任何人都知道如何为一个视图部分加载模型,然后将整个模型加载到另一个视图?EmberJS:为列表加载模型的传情,详细视图为完整模型

例如:

/* 
    This will get all projects, so we only want an id and name returned from the server. 
    Each project is a monster, so we don't want all data for each project. 
    */ 
    App.ProjectsRoute = Ember.Route.extend({ 
    model: function() { 
     return App.Project.find(); 
    } 
    }); 

    /* 
    This is a project detail, so we'd want the entire model returned from the server. Embedded records and all. 
    */ 
    App.ProjectRoute = Ember.Route.extend({ 

    }); 

我能找到最接近的事是这样的:https://stackoverflow.com/a/14183507/1125563

在,我可以做这样的事情:

App.ProjectRoute = Ember.Route.extend({ 
    setupController: function(controller, model) { 
     if (model.get('isTeaser')){ 
     model.reload(); 
     } 
    } 
    }); 

在这种解决办法,我有一个计算属性isTeaser,检查一些事情,以确定我是否只部分加载它。

除了一点点混乱之外,这里唯一的交易断路器是它正在转换到部分加载模型的路线,然后在它被加载之后,所有的东西都会优雅地嵌入。不是风扇。 。

我是否错过了一些明显的东西?

+1

你有没有看看新的**路由器整容**?详情请参阅https://gist.github.com/machty/5723945。我想引入的变化将帮助你解决你的问题。 – intuitivepixel

+0

你是对的 - 这会有很大的帮助。我认为afterModel钩子会让你看看它是否完整,如果没有,重新获取它,然后承诺将被解决..哪个RC是整容(或根本没有) – gcoladarci

+0

它已经在RC6 :) – intuitivepixel

回答

1

这是我的方法,它消除了初始渲染延迟。那是不是你的意思是'不合理'?

// Load the light version of all subjects on page load 
App.ApplicationController = Em.Controller.extend({ 
    init: function() { 
    return App.Subject.find(); 
    } 
}); 

// Fetch all our previously loaded subjects 
App.SubjectsRoute = Ember.Route.extend({ 
    model: function() { 
    return App.Subject.all(); 
    } 
}); 

App.SubjectRoute = Ember.Route.extend({ 
    // If we're loading the page directly to this route, do a normal find 
    model: function(params) { 
    return App.Subject.find(params.subject_id); 
    }, 
    setupController: function(controller, model) { 
    // Show what details we have for this subject (e.g. the title) immediately 
    controller.set("model", model); 

    // Load full details for the model and display them as soon as they arrive 
    if (Em.isEmpty(model.get("text"))) {  
     App.Subject.find(model.get("id")).then(function(model) { 
     return controller.set("model", model); 
     }); 
    } 
    } 
}); 
+0

这是有帮助的,但如果你有很多路线,我看不到在init中加载所有内容来解决问题。这就是说,我没有想到使用init来预加载我的模型,然后使用all()。感谢您的建议! – gcoladarci

+0

@drgizzlemd'if(App.Subject.all()。length == 0)App.Subject.find()'也许? – gunn