2014-11-02 42 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