Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

- Use a collection to populate Documents in #index

- Use event binding to refresh the Edit view via Document changes
  • Loading branch information...
commit 11a542e9e82f371580ee291bde13daef03dfb623 1 parent ad95932
James Yu authored committed
1  config/assets.yml
@@ -7,6 +7,7 @@ javascripts:
7 7 - public/javascripts/backbone.js
8 8 - public/javascripts/application.js
9 9 - public/javascripts/jquery.dotimeout.js
  10 + - public/javascripts/models/*.js
10 11 - public/javascripts/**/*.js
11 12
12 13 stylesheets:
1  public/javascripts/application.js
... ... @@ -1,6 +1,7 @@
1 1 var App = {
2 2 Views: {},
3 3 Controllers: {},
  4 + Collections: {},
4 5 init: function() {
5 6 new App.Controllers.Documents();
6 7 Backbone.history.start();
4 public/javascripts/collections/documents.js
... ... @@ -0,0 +1,4 @@
  1 +App.Collections.Documents = Backbone.Collection.extend({
  2 + model: Document,
  3 + url: '/documents'
  4 +});
11 public/javascripts/controllers/documents.js
@@ -19,11 +19,12 @@ App.Controllers.Documents = Backbone.Controller.extend({
19 19 },
20 20
21 21 index: function() {
22   - $.getJSON('/documents', function(data) {
23   - if(data) {
24   - var documents = _(data).map(function(i) { return new Document(i); });
25   - new App.Views.Index({ documents: documents });
26   - } else {
  22 + var documents = new App.Collections.Documents();
  23 + documents.fetch({
  24 + success: function() {
  25 + new App.Views.Index({ collection: documents });
  26 + },
  27 + error: function() {
27 28 new Error({ message: "Error loading documents." });
28 29 }
29 30 });
9 public/javascripts/views/edit.js
@@ -4,6 +4,8 @@ App.Views.Edit = Backbone.View.extend({
4 4 },
5 5
6 6 initialize: function() {
  7 + _.bindAll(this, 'render');
  8 + this.model.bind('change', this.render);
7 9 this.render();
8 10 },
9 11
@@ -14,11 +16,6 @@ App.Views.Edit = Backbone.View.extend({
14 16 this.model.save({ title: this.$('[name=title]').val(), body: this.$('[name=body]').val() }, {
15 17 success: function(model, resp) {
16 18 new App.Views.Notice({ message: msg });
17   -
18   - self.model = model;
19   - self.render();
20   - self.delegateEvents();
21   -
22 19 Backbone.history.saveLocation('documents/' + model.id);
23 20 },
24 21 error: function() {
@@ -46,5 +43,7 @@ App.Views.Edit = Backbone.View.extend({
46 43 $('#app').html(this.el);
47 44
48 45 this.$('[name=title]').val(this.model.get('title')); // use val, for security reasons
  46 +
  47 + this.delegateEvents();
49 48 }
50 49 });
5 public/javascripts/views/index.js
... ... @@ -1,13 +1,12 @@
1 1 App.Views.Index = Backbone.View.extend({
2 2 initialize: function() {
3   - this.documents = this.options.documents;
4 3 this.render();
5 4 },
6 5
7 6 render: function() {
8   - if(this.documents.length > 0) {
  7 + if(this.collection.models.length > 0) {
9 8 var out = "<h3><a href='#new'>Create New</a></h3><ul>";
10   - _(this.documents).each(function(item) {
  9 + this.collection.each(function(item) {
11 10 out += "<li><a href='#documents/" + item.id + "'>" + item.escape('title') + "</a></li>";
12 11 });
13 12 out += "</ul>";

0 comments on commit 11a542e

Please sign in to comment.
Something went wrong with that request. Please try again.