9951 explained code solutions for 126 technologies


backbone.jsHow do I use Backbone.js to create a Hacker News-style application?


Backbone.js is a JavaScript library that can be used to create a Hacker News-style application. To do this, you'll need to create a model to store data, a collection to store a group of models, and a view to display the data. Here's an example of how to create a model:

var NewsItem = Backbone.Model.extend({
  defaults: {
    title: '',
    content: '',
    comments: []
  }
});

This creates a model that has three default attributes: title, content, and comments.

Then you'll need to create a collection to store a group of NewsItem models:

var NewsItemsCollection = Backbone.Collection.extend({
  model: NewsItem
});

This creates a collection of NewsItem models.

Finally, you'll need to create a view to display the data:

var NewsItemView = Backbone.View.extend({
  tagName: 'li',
  template: _.template($('#news-item-template').html()),
  render: function(){
    this.$el.html(this.template(this.model.toJSON()));
    return this;
  }
});

This creates a view that will render a list item element with the model's data.

To use these components together, you'll need to create an instance of the NewsItemsCollection, add NewsItem models to it, and then create a NewsItemView for each model.

Helpful links

Edit this code on GitHub