2013-12-11 41 views
0

我有嵌套模型的結構是這樣的:嵌套模型數據控制器支持

App.Survey = DS.Model.extend({ 
     name: DS.attr('string'), 
     questions: DS.hasMany('question', {async: true}) 
    }); 

    App.Question = DS.Model.extend({ 
     questionName: DS.attr('string'), 
     parentQuestionId: DS.attr('number'), 
     position: DS.attr('number'), 
     questionLayoutId: DS.attr('number'), 
     questionLayoutName: DS.attr('string'), 
     childQuestions: DS.hasMany('question', {async: true}) 
    }); 

我有itemController成立有助於使控制器像添加額外的「屬性」的模型內容:

App.QuestionsController = Ember.ArrayController.extend({ 
     itemController: 'question' 
    }); 

    App.QuestionController = Ember.ObjectController.extend({ 
     needs: 'questions', 
     questions: Ember.computed.alias("controllers.questions"), 
     editMode: false, 
     hasChildren: function() { 
      return (this.get('childQuestions.length') > 0); 
     }.property('childQuestions'), 

     isBlockQuestion: function() { 
      return this.get('questionLayoutName') == "layout-block" 
     }.property('questionLayoutName') 
    }); 

所以當我參加調查時,我可以看到調查中的問題列表。我的路線是設置這樣的:

App.SurveyRoute = Ember.Route.extend({ 
     model: function (params) { 
      return this.get('store').find('survey', params.survey_id); 
     }, 

     setupController: function(controller, model){ 
      this._super(controller, model); 
      this.controllerFor('questions').set('model', model.get('questions')); 
     } 
    }); 

現在有了這個設置,我有項目控制器只有root級別問題的權力,但不是孩子的水平問題。我想知道是否有方法將模型數據綁定到適當的控制器上。

這裏是一個JSbin證明我的問題:http://jsbin.com/UROCObi/2/

這可能是有點過分進入,但這個概念是非常簡單的。一項調查可以有多個問題,一個問題本身可以有孩子的問題(在我的情況下稱爲塊問題)。正如你所看到的,我無法看到第三級的問題,因爲它沒有封裝在任何控制器中。我是否需要在SurveyRoute中爲所有嵌套級別的childQuestion實例化ArrayController,還是有其他更乾淨的方法來執行操作?

感謝, 迪

回答

1

您可以使用:

{{#each questions itemController="question"}} 
      ... 
      {{#each childQuestions itemController="childQuestion"}} 
        ... 
      {{/each}} 

    {{/each}} 

而且每each裏面的上下文是QuestionController和ChildQuestioncontroller的實例,respectevely(我不知道有關的命名慣例)。 不需要使用ArrayController,除非您還需要控制整個數組。

+0

感謝它在jsbin中工作,但我真正的應用程序我不知道爲什麼我得到「TypeError:this.parentController.childControllers是未定義的」錯誤!也許它只是需要徹底檢查。再次感謝。 –

+0

我認爲它與命名約定有關,以及Ember如何使用字符串'childQuestion'來查找Controller類並實例化它 – edpaez

+0

ok我發現我看到的問題是由ember-validation庫造成的! –