Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <h2 class="text-primary text-center">TwitchIt</h2>
  <hr/>
  <div id="appWrapper"></div>
</div>

<script id="appContent" type="text/template">
  <!--<input id="search" class='form-control' type="text" placeholder="Search Twitchers"/>
  <br>-->
  <div class="btn-group btn-group-justified">
    <label for="tab1" class='btn'>All</label>
    <label for="tab2" class='btn'>Online</label>
    <label for="tab3" class='btn'>Offline</label>
  </div>
  <hr>
  <div id='allUsersWrapper'></div>
</script>

<script id="allUsersContent" type="text/template">
  <ul class="list-unstyled" id='allUsersList'></ul>
</script>

<script id="userItem" type="text/template">
  <li class='row user-item'>
    <a href="<%= profileURL %>" target="_blank">
      <div class="col-xs-1">
        <img class='img-circle' width="35" src="<%= profileImg %>"/>
      </div>
      <b class='col-xs-8'>
        <%= name %><br>
        <i class='text-muted'><%= streamTitle %></i>
      </b>
      <i class="col-xs-3 glyphicon <%= streamStatus %>"></i>
    </a>
  </li>
</script>
              
            
!

CSS

              
                .container
  max-width: 500px
  label
    background: lightgray
    border: 1px solid #fff
  li.user-item
    margin: 15px 0
              
            
!

JS

              
                /*
TODO:
comeback to this code and optimize performance by removing old rendered views
*/
var clientID = 'pbfubrqgf1h2f7mpp71zxxvve4a7lnh',
    user = 'rlynjb08',
    users = 'https://api.twitch.tv/kraken/users/' + user + '/follows/channels?client_id=' + clientID;

/*
NOTE: instead of listing all users in an array. I'm just getting the list from
my twitch account
*/
var followedUser = Backbone.Model.extend();
var followedUsers = Backbone.Collection.extend({
  model: followedUser,
  url: users
});
// if returns null, means they are not currently streaming
// if data returns status 422 with message, means user removed their account
var isUserStreaming = Backbone.Model.extend({
  url: function() {
    return this.get('url')
  }
});

var appTwitchView = Backbone.View.extend({
  el: '#appWrapper',
  template: _.template( $('#appContent').html() ),
  initialize: function() {
    this.render();
  },
  render: function() {
    this.$el.html( this.template );
    this.auv = new allUsersWrapperView({ tabFlag: this.tabFlag });
  },
  events: {
    "click label[for]": 'userStatus'
    //'keyup #search': 'searchList'
  },
  userStatus: function(e) {
    this.tabFlag = e.currentTarget.htmlFor;
    this.auv.remove();
    this.initialize();
  }
  /*searchList: function(e) {
    this.searchFlag = e.target.value;
    console.log('searching', this.searchFlag);
    this.auv.remove();
    this.initialize();
  }*/
});

var allUsersWrapperView = Backbone.View.extend({
  el: '#allUsersWrapper',
  template: _.template( $('#allUsersContent').html() ),
  collection: new followedUsers,
  initialize: function(options) {
    this.tabFlag = options.tabFlag;
    this.$el.html( this.template );
    
    this.collection.fetch();
    this.listenTo(this.collection, 'sync', this.render);
  },
  render: function() {
    var items = this.collection.toJSON()[0].follows;
    _.each(items, function(model) {
      /*
      * Note: http://stackoverflow.com/questions/21819905/jquery-ajax-calls-in-for-loop
      * http://stackoverflow.com/questions/2405064/ajax-call-in-for-loop-wont-return-values-to-correct-array-positions
      * http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example
      */
      var title = model.channel.display_name,
          logo = model.channel.logo,
          url = model.channel.url,
          ajaxCall = 'https://api.twitch.tv/kraken/streams/' + title + '?client_id=' + clientID,
          stream = new isUserStreaming;
      
      stream.set({ url: ajaxCall });
      stream.fetch();
      this.listenTo(stream, 'sync', function(data) {
        var streamStatus = data.toJSON().stream,
            finalData = {
              name: title,
              profileImg: logo,
              profileURL: url,
              streamStatus: (streamStatus === null) ? 'glyphicon-off text-danger' : 'glyphicon-ok text-success',
              streamTitle: (streamStatus !== null) ? data.toJSON().stream.game : ''
            };
        var f = new allUsersView({ model: finalData, tabFlag: this.tabFlag });
      });
    }, this);
  }
});

var allUsersView = Backbone.View.extend({
  el: '#allUsersList',
  template: _.template( $('#userItem').html() ),
  initialize: function(options) {
    this.filter = options.tabFlag;
    this.filterTab();
  },
  filterTab: function() {
    if ( this.filter === undefined || this.filter === 'tab1' ) {
      this.$el.append( this.template( this.model ) );
    }
    if ( this.filter === 'tab2' ) {
      if (this.model.streamStatus === 'glyphicon-ok text-success') {
        this.$el.append( this.template( this.model ) );
      }
    }
    if ( this.filter === 'tab3' ) {
      if (this.model.streamStatus === 'glyphicon-off text-danger') {
        this.$el.append( this.template( this.model ) );
      }
    }
  }
});

var au = new appTwitchView();
              
            
!
999px

Console