2013-11-25 41 views
3

讓我解釋我的問題,我試圖直接從數據庫填充Ember.Select直接從數據庫填充Ember.Select

我有這些路線:

 this.resource('twod', function() { 
      this.resource('twoduser', { 
       path : ':user_id' 
      }); 
     }); 

twoduser,我顯示關於單個用戶的全部信息。在這個視圖中,我也有一個選擇框,最終用戶將選擇一個按鈕,然後用一個按鈕,他可以將用戶添加到他從Ember.Select中選擇的團隊。

我試圖做到這一點,

App.TwoduserController = Ember.ArrayController.extend({ 
    selectedTeam : null, 
    team : function(){ 
     var teams = []; 
     $.ajax({ 
      type : "GET", 
      url : "http://pioneerdev.us/users/getTeamNames", 
      data : data, 
      success : function (data){ 
       for (var i = 0; i < data.length; i ++){ 
        var teamNames = data[i]; 
        teams.push(teamNames); 
       } 
      } 
     }); 
     return teams; 
    }.property() 
}) 

然後我index.html

  {{view Ember.Select 
    contentBinding="team" 
    optionValuePath="teams.team_name" 
    optionLabelPath="teams.team_name" 
    selectionBinding="selectedTeam" 
    prompt="Please Select a Team"}} 

但我這樣做的時候,由於某種原因,它會干擾Twoduser,我無法查看單個用戶。

此外,這裏有一個樣本JSON迴應,我將通過網址獲得:

{"teams":[{"team_name":"Toronto Maple Leafs"},{"team_name":"Vancouver Canuck"}]} 

此外,我使用獲取所有用戶的Ajax這樣的:

App.Twod.reopenClass({ 
    findAll : function() { 

     return new Ember.RSVP.Promise(function(resolve, reject) { 
      $.getJSON("http://pioneerdev.us/users/index", function(data) { 
       var result = data.users.map(function(row) { 
        return App.Twod.create(row); 
       }); 
       resolve(result); 
      }).fail(reject); 
     }); 
    }, 
    findBy : function(user_id) { 

     return new Ember.RSVP.Promise(function(resolve, reject) { 
      var user = App.Twod.create(); 
      $.getJSON("http://pioneerdev.us/users/byId/" + user_id, function(data) { 
       var result = user.setProperties(data.user); 
       resolve(result); 
      }).fail(reject); 
     }); 
    } 
}); 

雖然有一件事,我有一個單獨的Teams路線:

this.resource('teamview', function(){ 
      this.resource('teamviewdetail', { 
       path : ':team_id' 
      }); 
     }); 

當您單擊一個團隊時,它會顯示所有團隊和一個團隊。

我可以使用TeamviewController嗎?或者我可以從Twoduser控制器中獲取團隊名稱,並像前面提到的那樣將名稱推送到數組中?

更多信息:

如果我用我提到的方式,我得到這個錯誤:

Uncaught TypeError: Object [object Object] has no method 'addArrayObserver' 

這裏有一個工作的問題jsfiddle我遇到。您可以從名稱&中選擇「Storyboard」,然後選擇該用戶。這將重現這個問題。

多一個更新:似乎使用ObjectController而不是ArrayController問題解決了addArrayObserver問題。但是我仍然無法讓Ember.Select中的球隊進入。

+0

App.TwoduserController.team需要'返回球隊;'線 –

+0

如果我用這個方法,我得到的錯誤,遺漏的類型錯誤:對象的翻譯:有沒有方法「addArrayObserver」 – user1601973

回答

2

這裏最大的問題是您使用Array#push而不是pushObject。 Ember需要特殊的方法來了解變化。否則,它會繼續認爲這組隊伍像你第一次返回時一樣空。第二大問題是您的ajax success調用不能正確訪問返回的數據。

而且,optionValuePathoptionLabelPath與單個選擇選項視圖有關,所以它們應以content開頭,這是視圖上設置的單個項目。所以:content.team_name

+0

如果你檢查我小提琴,http://jsfiddle.net/qgcB7/3/。我嘗試使用'pushObject'。其他的東西可能是錯的。 – user1601973