Skip to content

Instantly share code, notes, and snippets.

@wearhere
Last active February 17, 2022 01:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wearhere/7847accfa9555723f863861cc4eb74b6 to your computer and use it in GitHub Desktop.
Save wearhere/7847accfa9555723f863861cc4eb74b6 to your computer and use it in GitHub Desktop.
Backbone + Handlebars + Rollup
const handlebars = require('rollup-plugin-handlebars-plus');
// Rollup plugins
let plugins = [
handlebars({
templateExtension: '.html',
helpers: ['/utils/HandlebarsHelpers'],
jquery: 'jquery'
})
];
export default function(Handlebars) {
Handlebars.registerHelper('titlecase', (string) => {
if (!string) return string;
return string[0].toUpperCase() + string.slice(1);
});
}
<h1>Hello, {{titlecase name}}</h1>
const HelloWorldModel = Backbone.Model.extend({
defaults: {
name: null
}
});
import Template from './HelloWorld.html';
const HelloWorldView = Backbone.View.extend({
initialize() {
this.listenTo(this.model, 'change', this.render);
},
render() {
this.$el.html(Template(this.model.attributes));
return this;
}
});
<!DOCTYPE html>
<html>
<head>
<script src="//cdn.jsdelivr.net/g/backbonejs@1.3.3,underscorejs@1.8.3,jquery@3.1.1,handlebarsjs@4.0.5(handlebars.runtime.min.js)"></script>
</head>
<body>
<div id="main"></div>
</body>
</html>
import HelloWorldModel from './HelloWorldModel.js';
import HelloWorldView from './HelloWorldView.js';
let model = new HelloWorldModel({ name: 'jeff' });
new HelloWorldView({
el: $('#main'),
model
}).render();
setTimeout(() => {
model.set('name', 'brad');
}, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment