Last active
February 17, 2022 01:37
-
-
Save wearhere/7847accfa9555723f863861cc4eb74b6 to your computer and use it in GitHub Desktop.
Backbone + Handlebars + Rollup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const handlebars = require('rollup-plugin-handlebars-plus'); | |
// Rollup plugins | |
let plugins = [ | |
handlebars({ | |
templateExtension: '.html', | |
helpers: ['/utils/HandlebarsHelpers'], | |
jquery: 'jquery' | |
}) | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function(Handlebars) { | |
Handlebars.registerHelper('titlecase', (string) => { | |
if (!string) return string; | |
return string[0].toUpperCase() + string.slice(1); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Hello, {{titlecase name}}</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const HelloWorldModel = Backbone.Model.extend({ | |
defaults: { | |
name: null | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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