Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

more work

  • Loading branch information...
commit 6b0b175808c2baca92867083040c83b308ddcfdc 1 parent ca156a7
authored jamesyu committed
4  app/models/document.rb
... ...
@@ -1,3 +1,7 @@
1 1
 class Document < ActiveRecord::Base
2 2
   attr_accessible :body, :title
  3
+  
  4
+  def to_json(options = {})
  5
+    super(options.merge(:only => [ :id, :title, :created_at, :body ]))
  6
+  end
3 7
 end
2  app/views/home/index.html.erb
... ...
@@ -1,6 +1,8 @@
1 1
 <h1><a href="#">CloudEdit</a></h1>
2 2
 <h2>A Backbone.js Rails Example by James Yu</h2>
3 3
 
  4
+<div id="notice"></div>
  5
+
4 6
 <div id="app"></div>
5 7
 
6 8
 <script type="text/javascript">
1  config/assets.yml
@@ -5,6 +5,7 @@ javascripts:
5 5
     - public/javascripts/underscore.js
6 6
     - public/javascripts/backbone.js
7 7
     - public/javascripts/application.js
  8
+    - public/javascripts/jquery.dotimeout.js
8 9
     - public/javascripts/**/*.js
9 10
 
10 11
 stylesheets:
1  public/javascripts/application.js
@@ -6,4 +6,3 @@ var App = {
6 6
         Backbone.history.start();
7 7
     }
8 8
 };
9  
-
8  public/javascripts/controllers/documents.js
... ...
@@ -1,15 +1,15 @@
1 1
 App.Controllers.Documents = Backbone.Controller.extend({
2 2
     routes: {
3  
-        "documents/:id":            "show",
  3
+        "documents/:id":            "edit",
4 4
         "":                         "index",
5 5
         "new":                      "newDoc"
6 6
     },
7 7
     
8  
-    show: function(id) {
  8
+    edit: function(id) {
9 9
         var doc = new Document({ id: id });
10 10
         doc.fetch({
11 11
             success: function(model, resp) {
12  
-                new App.Views.Show({ model: doc });
  12
+                new App.Views.Edit({ model: doc });
13 13
             },
14 14
             error: function() {
15 15
                 new Error({ message: 'Could not find that document.' });
@@ -30,6 +30,6 @@ App.Controllers.Documents = Backbone.Controller.extend({
30 30
     },
31 31
     
32 32
     newDoc: function() {
33  
-        new App.Views.Show({ model: new Document() });
  33
+        new App.Views.Edit({ model: new Document() });
34 34
     }
35 35
 });
9  public/javascripts/jquery.dotimeout.js
... ...
@@ -0,0 +1,9 @@
  1
+/*
  2
+ * jQuery doTimeout: Like setTimeout, but better! - v1.0 - 3/3/2010
  3
+ * http://benalman.com/projects/jquery-dotimeout-plugin/
  4
+ * 
  5
+ * Copyright (c) 2010 "Cowboy" Ben Alman
  6
+ * Dual licensed under the MIT and GPL licenses.
  7
+ * http://benalman.com/about/license/
  8
+ */
  9
+(function($){var a={},c="doTimeout",d=Array.prototype.slice;$[c]=function(){return b.apply(window,[0].concat(d.call(arguments)))};$.fn[c]=function(){var f=d.call(arguments),e=b.apply(this,[c+f[0]].concat(f));return typeof f[0]==="number"||typeof f[1]==="number"?this:e};function b(l){var m=this,h,k={},g=l?$.fn:$,n=arguments,i=4,f=n[1],j=n[2],p=n[3];if(typeof f!=="string"){i--;f=l=0;j=n[1];p=n[2]}if(l){h=m.eq(0);h.data(l,k=h.data(l)||{})}else{if(f){k=a[f]||(a[f]={})}}k.id&&clearTimeout(k.id);delete k.id;function e(){if(l){h.removeData(l)}else{if(f){delete a[f]}}}function o(){k.id=setTimeout(function(){k.fn()},j)}if(p){k.fn=function(q){if(typeof p==="string"){p=g[p]}p.apply(m,d.call(n,i))===true&&!q?o():e()};o()}else{if(k.fn){j===undefined?e():k.fn(j===false);return true}else{e()}}}})(jQuery);
2  public/javascripts/models/document.js
@@ -4,4 +4,4 @@ var Document = Backbone.Model.extend({
4 4
       if (this.isNew()) return base;
5 5
       return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
6 6
     }
7  
-});
  7
+});
10  public/javascripts/views/show.js → public/javascripts/views/edit.js
... ...
@@ -1,4 +1,4 @@
1  
-App.Views.Show = Backbone.View.extend({
  1
+App.Views.Edit = Backbone.View.extend({
2 2
     events: {
3 3
         "submit form": "save"
4 4
     },
@@ -9,12 +9,20 @@ App.Views.Show = Backbone.View.extend({
9 9
     
10 10
     save: function() {
11 11
         var self = this;
  12
+        var msg = this.model.isNew() ? 'Succesfully created!' : "Saved!";
  13
+        
12 14
         this.model.save({ title: this.$('[name=title]').val(), body: this.$('[name=body]').val() }, {
13 15
             success: function(model, resp) {
  16
+                new App.Views.Notice({ message: msg });
  17
+                
14 18
                 self.model = model;
15 19
                 self.render();
16 20
                 self.delegateEvents();
  21
+
17 22
                 Backbone.history.saveLocation('documents/' + model.id);
  23
+            },
  24
+            error: function() {
  25
+                new App.Views.Error();
18 26
             }
19 27
         });
20 28
         
4  public/javascripts/views/index.js
@@ -8,7 +8,7 @@ App.Views.Index = Backbone.View.extend({
8 8
         if(this.documents.length > 0) {
9 9
             var out = "<h3><a href='#new'>Create New</a></h3><ul>";
10 10
             _(this.documents).each(function(item) {
11  
-                out += "<li><a href='#documents/" + item.id + "'>" + item.get('title') + "</a></li>";
  11
+                out += "<li><a href='#documents/" + item.id + "'>" + item.escape('title') + "</a></li>";
12 12
             });
13 13
             out += "</ul>";
14 14
         } else {
@@ -17,4 +17,4 @@ App.Views.Index = Backbone.View.extend({
17 17
         $(this.el).html(out);
18 18
         $('#app').html(this.el);
19 19
     }
20  
-});
  20
+});
33  public/javascripts/views/notice.js
... ...
@@ -0,0 +1,33 @@
  1
+App.Views.Notice = Backbone.View.extend({
  2
+    className: "success",
  3
+    displayLength: 5000,
  4
+    defaultMessage: '',
  5
+    
  6
+    initialize: function() {
  7
+        _.bindAll(this, 'render');
  8
+        this.message = this.options.message || this.defaultMessage;
  9
+        this.render();
  10
+    },
  11
+    
  12
+    render: function() {
  13
+        var view = this;
  14
+        
  15
+        $(this.el).html(this.message);
  16
+        $(this.el).hide();
  17
+        $('#notice').html(this.el);
  18
+        $(this.el).slideDown();
  19
+        $.doTimeout(this.displayLength, function() {
  20
+            $(view.el).slideUp();
  21
+            $.doTimeout(2000, function() {
  22
+                view.remove();
  23
+            });
  24
+        });
  25
+        
  26
+        return this;
  27
+    }
  28
+});
  29
+
  30
+App.Views.Error = App.Views.Notice.extend({
  31
+    className: "error",
  32
+    defaultMessage: 'Uh oh! Something went wrong. Please try again.'
  33
+});

0 notes on commit 6b0b175

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