Skip to content

Instantly share code, notes, and snippets.

@taylor-smith
Created August 1, 2014 21:43
Show Gist options
  • Save taylor-smith/67e071cbeabff64d0675 to your computer and use it in GitHub Desktop.
Save taylor-smith/67e071cbeabff64d0675 to your computer and use it in GitHub Desktop.
var TodoItem = Backbone.Model.extend({});
var TodoView = Backbone.View.extend({
render: function(){
var html = '<h3>' + this.model.get('description') + '</h3>'
this.$el.html(html);
return this;
}
});
var todoItem = new TodoItem(
{ description: "Pick up milk",
status: "incomplete",
id: 1 }
);
var todoView = new TodoView({ model: todoItem })
console.log(todoView.el);
@clamstew
Copy link

clamstew commented Aug 1, 2014

Check out this file - https://github.com/clamstew/backbone-todo-app/blob/master/app/assets/javascripts/app/views/todo-list-view.js

Basically, when you created a new instance of the View object, you need to pass it an "el" or it won't know where to put things on the DOM.

So something like:

var TodoItem = Backbone.Model.extend({});

var todoItem = new TodoItem({ 
  description: "Pick up milk",
  status: "incomplete",
  id: 1
});

var TodoView = Backbone.View.extend({
  render: function(){
    var html = '<h3>' + this.model.get('description') + '</h3>'
    this.$el.html(html);
    return this;
  }
});

var todoView = new TodoView({ 
  model: todoItem, 
  el: ".main" // <-------------------- here
});

console.log(todoView.el);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment