Instantly share code, notes, and snippets.
Save kyleondata/3440492 to your computer and use it in GitHub Desktop.
<!DOCTYPE html> | |
<html> | |
<head> | |
<metahttp-equiv="Content-Type" | |
content="text/html; charset=utf-8"/> | |
<scriptsrc="jquery-1.7.2.min.js"></script> | |
<scriptsrc="handlebars-1.0.0.beta.6.js"></script> | |
<scriptsrc="underscore-min.js"></script> | |
<scriptsrc="backbone-min.js"></script> | |
<!-- Setup our templates --> | |
<scriptid="PersonTemplate"type="text/x-handlebars-template"> | |
<div> | |
{{name}} | |
{{age}} | |
<div> | |
</script> | |
<!-- | |
Note the [] this is important | |
because handlebars and backbone collections | |
dont play well with each other in regards | |
to naming JSON groups | |
--> | |
<scriptid="PeopleTemplate"type="text/x-handlebars-template"> | |
<div> | |
{{#each[]}} | |
{{this.name}} | |
{{this.age}} | |
<br/> | |
{{/each}} | |
<div> | |
</script> | |
<!-- End templates setup --> | |
<script> | |
// Stub out the person model | |
varPerson=Backbone.Model.extend({ | |
}); | |
// Create a collection of persons | |
varPeople=Backbone.Collection.extend({ | |
model:Person, | |
}); | |
// Define the view for a single person | |
varPersonView=Backbone.View.extend({ | |
render:function(){ | |
// This is method that can be called | |
// once an object is init. You could | |
// also do this in the initialize event | |
varsource=$('#PersonTemplate').html(); | |
vartemplate=Handlebars.compile(source); | |
varhtml=template(this.model.toJSON()); | |
this.$el.html(html); | |
} | |
}); | |
// Define the view for People | |
varPeopleView=Backbone.View.extend({ | |
render:function(){ | |
// This is method that can be called | |
// once an object is init. You could | |
// also do this in the initialize event | |
varsource=$('#PeopleTemplate').html(); | |
vartemplate=Handlebars.compile(source); | |
varhtml=template(this.collection.toJSON()); | |
this.$el.html(html); | |
}, | |
initialize:function(){ | |
this.collection.on('add',this.render,this) | |
} | |
}); | |
//THANKS Rameş Aliyev for the feedback on making this cleaner | |
// https://gist.github.com/4682984 | |
// Create instance of People Collection | |
varpeople=newPeople(); | |
// Create new instances of the person models | |
varperson=newPerson({name:"Tim",age:5}); | |
varperson2=newPerson({name:"Jill",age:15}); | |
// Create instances of the views | |
varpersonView=newPersonView({ | |
model:person | |
}); | |
varpeopleView=newPeopleView({ | |
collection:people | |
}); | |
$(document).ready(function(){ | |
// We have to do this stuff in the dom ready | |
// so that the container objects get built out | |
// Set el of the views. | |
personView.el=$('#personContainer'); | |
peopleView.el=$('#peopleContainer'); | |
// Add them to a new instance of the people collection | |
people.add(person); | |
people.add(person2); | |
// Render the views. If you are using the initialize | |
// method then you do not have to do this step. | |
personView.render(); | |
//peopleView.render(); | |
// Try on console! | |
// people.add(new Person({name: 'Rames', age:'23'})); | |
}); | |
</script> | |
</head> | |
<body> | |
<divid='personContainer'></div> | |
<hr> | |
<divid='peopleContainer'></div> | |
</body> | |
</html> |
anthonybrown commentedDec 29, 2013
there's a typo, instead of personView.el = $('#personConatiner')
should be: personView.$el = $('#personContainer')
same for the peopleView.$el
I am using the latest versions, jQuery 2.0, Backbone 1.01 etc
suark commentedJan 15, 2014
Will this work if I am trying it in JS bin? I changed the dependencies so it uses the links that jsbin needs. But the output just kind of displays the top of some box.... and did I mention I'm new to handlebars?
shearichard commentedApr 3, 2017
Thanks for this. It allowed me to see that in order to use handlebars you must still include underscore. I'm not sure why that is but I had assumed you swopped out underscore when using handlebar and all sorts of pain resulted.
mohammod7009 commentedJul 6, 2019
Thanks a lot. This example really helped me!
FYI - I had to update the following line as per the other post athttps://gist.github.com/4682984
I had to change this.$el.html(html);
to $(this.el).html(html);