Permalink
Browse files

more work

  • Loading branch information...
1 parent ca156a7 commit 6b0b175808c2baca92867083040c83b308ddcfdc James Yu committed with Feb 1, 2011
View
@@ -1,3 +1,7 @@
class Document < ActiveRecord::Base
attr_accessible :body, :title
+
+ def to_json(options = {})
+ super(options.merge(:only => [ :id, :title, :created_at, :body ]))
+ end
end
@@ -1,6 +1,8 @@
<h1><a href="#">CloudEdit</a></h1>
<h2>A Backbone.js Rails Example by James Yu</h2>
+<div id="notice"></div>
+
<div id="app"></div>
<script type="text/javascript">
View
@@ -5,6 +5,7 @@ javascripts:
- public/javascripts/underscore.js
- public/javascripts/backbone.js
- public/javascripts/application.js
+ - public/javascripts/jquery.dotimeout.js
- public/javascripts/**/*.js
stylesheets:
@@ -6,4 +6,3 @@ var App = {
Backbone.history.start();
}
};
-
@@ -1,15 +1,15 @@
App.Controllers.Documents = Backbone.Controller.extend({
routes: {
- "documents/:id": "show",
+ "documents/:id": "edit",
"": "index",
"new": "newDoc"
},
- show: function(id) {
+ edit: function(id) {
var doc = new Document({ id: id });
doc.fetch({
success: function(model, resp) {
- new App.Views.Show({ model: doc });
+ new App.Views.Edit({ model: doc });
},
error: function() {
new Error({ message: 'Could not find that document.' });
@@ -30,6 +30,6 @@ App.Controllers.Documents = Backbone.Controller.extend({
},
newDoc: function() {
- new App.Views.Show({ model: new Document() });
+ new App.Views.Edit({ model: new Document() });
}
});

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -4,4 +4,4 @@ var Document = Backbone.Model.extend({
if (this.isNew()) return base;
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
}
-});
+});
@@ -1,4 +1,4 @@
-App.Views.Show = Backbone.View.extend({
+App.Views.Edit = Backbone.View.extend({
events: {
"submit form": "save"
},
@@ -9,12 +9,20 @@ App.Views.Show = Backbone.View.extend({
save: function() {
var self = this;
+ var msg = this.model.isNew() ? 'Succesfully created!' : "Saved!";
+
this.model.save({ title: this.$('[name=title]').val(), body: this.$('[name=body]').val() }, {
success: function(model, resp) {
+ new App.Views.Notice({ message: msg });
+
self.model = model;
self.render();
self.delegateEvents();
+
Backbone.history.saveLocation('documents/' + model.id);
+ },
+ error: function() {
+ new App.Views.Error();
}
});
@@ -8,7 +8,7 @@ App.Views.Index = Backbone.View.extend({
if(this.documents.length > 0) {
var out = "<h3><a href='#new'>Create New</a></h3><ul>";
_(this.documents).each(function(item) {
- out += "<li><a href='#documents/" + item.id + "'>" + item.get('title') + "</a></li>";
+ out += "<li><a href='#documents/" + item.id + "'>" + item.escape('title') + "</a></li>";
});
out += "</ul>";
} else {
@@ -17,4 +17,4 @@ App.Views.Index = Backbone.View.extend({
$(this.el).html(out);
$('#app').html(this.el);
}
-});
+});
@@ -0,0 +1,33 @@
+App.Views.Notice = Backbone.View.extend({
+ className: "success",
+ displayLength: 5000,
+ defaultMessage: '',
+
+ initialize: function() {
+ _.bindAll(this, 'render');
+ this.message = this.options.message || this.defaultMessage;
+ this.render();
+ },
+
+ render: function() {
+ var view = this;
+
+ $(this.el).html(this.message);
+ $(this.el).hide();
+ $('#notice').html(this.el);
+ $(this.el).slideDown();
+ $.doTimeout(this.displayLength, function() {
+ $(view.el).slideUp();
+ $.doTimeout(2000, function() {
+ view.remove();
+ });
+ });
+
+ return this;
+ }
+});
+
+App.Views.Error = App.Views.Notice.extend({
+ className: "error",
+ defaultMessage: 'Uh oh! Something went wrong. Please try again.'
+});

0 comments on commit 6b0b175

Please sign in to comment.