2014-11-02 24 views
1

我一整天都在這裏,顯然這裏有一些基本的錯誤,請幫我理解它是什麼。Ember上下文的錯誤理解

我試圖創建一個YouTube播放列表應用程序,作爲第一步我試圖搜索YouTube的相關視頻,並返回結果到我的模型這條路線。

我唯一知道的肯定是搜索功能工作正常,因爲我從另一個項目中拿走了。也有一些是錯誤的,我把值代入模型的方式..

這是我走到這一步:

app.js

App = Ember.Application.create({}); 

App.Router.map(function() { 
    this.resource('SearchYT'); 
    this.resource('Play'); 
}); 

App.IndexRoute = Ember.Route.extend({ 
    redirect : function() { 
     this.transitionTo('SearchYT'); 
    } 
}); 

App.SearchYTRoute = Ember.Route.extend({ 
    model : function() { 
     return App.Video.all(); 
    } 
}); 

App.Video = Ember.Object.extend({ 
    title : null, 
    seconds : null, 
    yid : null 
}); 

App.Video.reopenClass({ 
    // there are no videos initially 
    content : [], 

    // Searching flag 
    isSearching : false, 

    actions : { 
     makeSearch : function() { 
      console.log('working1'); 

      // Start searching and remove existing results 
      this.set('isSearching', true); 
      this.set('content', []); 

      var query = this.get('searchString'); 
      var c = $.getJSON("http://gdata.youtube.com/feeds/api/videos", { 
       alt : 'json', 
       'max-results' : 10, 
       v : 2, 
       q : query 
      }); 
      c.success(function(data) { 
       var entries = data.feed.entry, results = []; 

       for (var i = 0; i < entries.length; i++) { 
        var e = entries[i]; 
        results.push(App.Video.create({ 
         yid : e.id.$t.split(':')[3], 
         seconds : parseInt(e.media$group.yt$duration.seconds), 
         title : e.title.$t 
        })); 
       } 
       this.set('content', results); 
      }); 

      c.complete(function() { 
       this.set('isSearching', false); 
      }); 
     } 
    } 
}); 

請幫助我理解我的問題,謝謝 提前。

回答

1

這不是真正的Ember問題,當您嘗試設置內容時,您超出了範圍。我已經更新了代碼,顯示了兩種處理方法,一種保留對此的引用,另一種保留對數組的引用。

makeSearch : function() { 
     console.log('working1'); 

     // Start searching and remove existing results 
     // keep a reference to the new array, then use `pushObject` 
     var newResults = [], 
      self = this; 
     this.set('isSearching', true); 
     this.set('content', newResults); 

     var query = this.get('searchString'); 
     var c = $.getJSON("http://gdata.youtube.com/feeds/api/videos", { 
      alt : 'json', 
      'max-results' : 10, 
      v : 2, 
      q : query 
     }); 
     c.success(function(data) { 
      var entries = data.feed.entry, results = []; 

      for (var i = 0; i < entries.length; i++) { 
       var e = entries[i]; 
       newResults.pushObject(App.Video.create({ 
        yid : e.id.$t.split(':')[3], 
        seconds : parseInt(e.media$group.yt$duration.seconds), 
        title : e.title.$t 
       })); 
      } 
      // this.set('content', results); <-- this here is not the this out of the success 
     }); 

     c.complete(function() { 
      self.set('isSearching', false); // <-- same here, this is different 
     }); 
    } 
+0

非常感謝您的回覆!它仍然劑量工作雖然..現在它告訴我以下幾點: 處理路由時:SearchYT undefined不是一個函數TypeError:undefined不是函數 – 2014-11-02 17:15:55

+0

'all'沒有在App.Video上定義。你正在調用'App.Video.all()',但你從來沒有定義過'all'。 – Kingpin2k 2014-11-02 17:32:05

+0

所以基本上改變它的內容應該做的伎倆? – 2014-11-02 17:34:57