Getting YouTube views with Backbone.js

I am using Backbone.js to mediate between Wordpress (using the super rad JSON API plugin) and my frontend.

Each of my Wordpress posts has a YouTube video ID attached to it, which pulls back when I grab the collection.

I am trying to work out the best point in the application to go out and parse the video id to get the overall video.

This is the code that retrieves the collection model:

parse: function(response) {
  return response.posts;


And the following code then displays the corresponding view.

this.getCollection().each(function(model) {
  var view = new App.Thumbnail.View({model: model});
  var item = $(view.render().el);


At some point in this process, I need the thumbnails to assign themselves the video count. This is the code I am using to grab YouTube views:

function getYoutubeViewCount(videoID) {
    url: "" + videoID + "?v=2&alt=json",
    dataType: "jsonp",
    success: function (data) { console.log($statistics.viewCount); }


So my question is, where do you think is the best place to count views? Should I create a submodule that will select the account? Or do it outside of backbone.js?

Any help would not be appreciated!

Thanks in advance, Charlie.


source to share

1 answer


should be an attribute of your video model subclass. You can load both the video wordpress data and the YouTube counter in parallel before instantiating your model, or you can instantiate your model using the wordpress data while the YouTube data is fetched and added. After you get the number of views from youtube, set

on the corresponding model instance. Imagine that your view is bound to an event change:viewCount

on the model and re-renders itself (or at least drags the view count to the appropriate element in the DOM) after that data has loaded.

As far as WHEN is concerned, you will, perhaps you should start fetching the number of views in the background as soon as you instantiate the base video model and populate it with basic attributes from your wordpress datasource. Or you can load the views later on lazily when the video is playing or scrolling in the viewport or fading or whatever.



All Articles