Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use Underscore templating

  • Loading branch information...
commit 784afdbd2ab520f2a183bb20723a1139dbb8bc65 1 parent 11a542e
James Yu authored committed
View
9 app/views/documents/document.jst
@@ -0,0 +1,9 @@
+<form>
+ <label for='title'>Title</label>
+ <input name='title' type='text' />
+
+ <label for='body'>Body</label>
+ <textarea name='body'><%= model.get('body') %></textarea>
+
+ <button><%= model.isNew() ? 'Create' : 'Save' %></button>
+</form>
View
9 app/views/documents/documents_collection.jst
@@ -0,0 +1,9 @@
+<% if(collection.models.length > 0) { %>
+ <h3><a href='#new'>Create New</a></h3><ul>
+ <% collection.each(function(item) { %>
+ <li><a href='#documents/<%= item.id %>'><%= item.escape('title') %></a></li>
+ <% }); %>
+ </ul>
+<% } else { %>
+ <h3>No documents! <a href='#new'>Create one</a></h3>
+<% } %>
View
2  config/assets.yml
@@ -1,4 +1,5 @@
package_assets: off
+template_function: _.template
javascripts:
app:
- public/javascripts/jquery-1.4.4.min.js
@@ -9,6 +10,7 @@ javascripts:
- public/javascripts/jquery.dotimeout.js
- public/javascripts/models/*.js
- public/javascripts/**/*.js
+ - app/views/**/*.jst
stylesheets:
app:
View
17 public/javascripts/views/edit.js
@@ -27,22 +27,11 @@ App.Views.Edit = Backbone.View.extend({
},
render: function() {
- var out = '<form>';
- out += "<label for='title'>Title</label>";
- out += "<input name='title' type='text' />";
-
- out += "<label for='body'>Body</label>";
- out += "<textarea name='body'>" + (this.model.escape('body') || '') + "</textarea>";
-
- var submitText = this.model.isNew() ? 'Create' : 'Save';
-
- out += "<button>" + submitText + "</button>";
- out += "</form>";
-
- $(this.el).html(out);
+ $(this.el).html(JST.document({ model: this.model }));
$('#app').html(this.el);
- this.$('[name=title]').val(this.model.get('title')); // use val, for security reasons
+ // use val to fill in title, for security reasons
+ this.$('[name=title]').val(this.model.get('title'));
this.delegateEvents();
}
View
11 public/javascripts/views/index.js
@@ -4,16 +4,7 @@ App.Views.Index = Backbone.View.extend({
},
render: function() {
- if(this.collection.models.length > 0) {
- var out = "<h3><a href='#new'>Create New</a></h3><ul>";
- this.collection.each(function(item) {
- out += "<li><a href='#documents/" + item.id + "'>" + item.escape('title') + "</a></li>";
- });
- out += "</ul>";
- } else {
- out = "<h3>No documents! <a href='#new'>Create one</a></h3>";
- }
- $(this.el).html(out);
+ $(this.el).html(JST.documents_collection({ collection: this.collection }));
$('#app').html(this.el);
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.