Last active
December 14, 2015 10:59
-
-
Save mitsuruog/5076060 to your computer and use it in GitHub Desktop.
Backbone with handlebar.js
This file contains hidden or 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
app | |
├ Gruntfile.js | |
├ hbs | |
│ └ partial.hbs //テンプレート | |
└ js | |
├ views | |
│ └ partial.js //テンプレートをレンダリングするView | |
├ collections | |
├ models | |
├ template.js //テンプレートがプリコンパイルされたJS | |
├ namespace.js | |
└ app.js |
This file contains hidden or 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
module.exports = function(grunt) { | |
grunt.initConfig({ | |
//some... | |
handlebars: { | |
compile: { | |
options: { | |
namespace: "MyApp.Templates", | |
processName: function(filepath) { // input -> app/hbs/partial.hbs | |
var pieces = filepath.split("/"); | |
return pieces[pieces.length - 1].replace(/.hbs$/ , ''); //output -> partial | |
} | |
}, | |
files: { | |
"app/js/template.js": "app/hbs/*.hbs" | |
} | |
} | |
} | |
}); | |
// Load the plugin. | |
//some tasks... | |
grunt.loadNpmTasks('grunt-contrib-handlebars'); | |
// Default task(s). | |
grunt.registerTask('default', ['handlebars']); | |
}; |
This file contains hidden or 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
var MyApp = { | |
Views: {}, | |
Collections: {}, | |
Models: {}, | |
Templates: {} | |
} |
This file contains hidden or 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
MyApp.Views.partial = Backbone.View.extend({ | |
//partial.hbsで定義したテンプレート | |
tmpl: MyApp.Templates.partial, | |
//some... | |
render: function() { | |
this.$el.html( | |
this.tmpl({ | |
models: this.collection.toJSON() | |
}) | |
); | |
}, | |
//some... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment